Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 mysql query help, help me

views
     
TSphyckeryz
post May 15 2017, 10:01 AM, updated 9y ago

Getting Started
**
Junior Member
69 posts

Joined: Jan 2013
From: Malaysia

hi master.. i hope can ask guide from u guys here..

my issue here how to sum data in one of table field and insert into second table the total value.. sweat.gif sweat.gif
for ur easy understand i put the sample table figure below.. icon_question.gif icon_question.gif

Attached Image

notworthy.gif notworthy.gif how to query master.. icon_question.gif icon_question.gif
ubsacc2004
post May 15 2017, 10:06 AM

No life without MUsic
******
Senior Member
1,860 posts

Joined: Sep 2006
From: KL


QUOTE(phyckeryz @ May 15 2017, 10:01 AM)
hi master.. i hope can ask guide from u guys here..

my issue here how to sum data in one of table field and insert into second table the total value..  sweat.gif  sweat.gif
for ur easy understand i put the sample table figure below..  icon_question.gif  icon_question.gif

Attached Image

notworthy.gif  notworthy.gif how to query master.. icon_question.gif  icon_question.gif
*
SELECT SUM(harga) as harga, ID FROM Table1 into #TEMP1 GROUP BY ID

insert into table2
(select * from #TEMP1)

just for the concept . u need test it out as i`m using MSSQL and visual foxpro haha.

TSphyckeryz
post May 15 2017, 10:14 AM

Getting Started
**
Junior Member
69 posts

Joined: Jan 2013
From: Malaysia

QUOTE(ubsacc2004 @ May 15 2017, 10:06 AM)
SELECT SUM(harga) as harga, ID FROM Table1 into #TEMP1 GROUP BY ID

insert into table2
(select * from #TEMP1)

just for the concept . u need test it out as i`m using MSSQL and visual foxpro haha.
*
whats mean by #TEMP1, can be any name?
i have think to do this below but not work..
(select sum(harga) as totalA from table1 where id='a' and insert into table2 (total)values (totalA) )
**such a mess query cry.gif cry.gif
Auraguy
post May 15 2017, 10:30 AM

Getting Started
**
Junior Member
297 posts

Joined: Nov 2006


insert into table2 ('a', (select sum(harga) from table1 where id = 'a')

Not sure if can if query in a query works or not, but u can try.
TSphyckeryz
post May 15 2017, 10:37 AM

Getting Started
**
Junior Member
69 posts

Joined: Jan 2013
From: Malaysia

QUOTE(Auraguy @ May 15 2017, 10:30 AM)
insert into table2 ('a', (select sum(harga) from table1 where id = 'a')

Not sure if can if query in a query works or not, but u can try.
*
thanks for ur help.. thumbup.gif let me try first..
alien3d
post May 15 2017, 11:09 AM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(phyckeryz @ May 15 2017, 10:01 AM)
hi master.. i hope can ask guide from u guys here..

my issue here how to sum data in one of table field and insert into second table the total value..  sweat.gif  sweat.gif
for ur easy understand i put the sample table figure below..  icon_question.gif  icon_question.gif

Attached Image

notworthy.gif  notworthy.gif how to query master.. icon_question.gif  icon_question.gif
*
CODE

INSERT INTO `table2` ('a',(
 SELECT SUM(harga)
 FROM `table1`
 WHERE id = 'a'
 GROUP BY `a`
)
);


Dear ts.. sila rajin rajin guna tool macam
http://sqlfiddle.com/
whistling.gif

This post has been edited by alien3d: May 15 2017, 11:10 AM
TSphyckeryz
post May 15 2017, 11:35 AM

Getting Started
**
Junior Member
69 posts

Joined: Jan 2013
From: Malaysia

QUOTE(Auraguy @ May 15 2017, 10:30 AM)
insert into table2 ('a', (select sum(harga) from table1 where id = 'a')

Not sure if can if query in a query works or not, but u can try.
*
QUOTE(alien3d @ May 15 2017, 11:09 AM)
CODE

INSERT INTO `table2` ('a',(
 SELECT SUM(harga)
 FROM `table1`
 WHERE id = 'a'
 GROUP BY `a`
)
);


Dear ts.. sila rajin rajin guna  tool macam
http://sqlfiddle.com/
whistling.gif
*
oh god.. tq very much..
trimas atas nasihat siffuu.. icon_rolleyes.gif
rclxm9.gif rclxm9.gif
ubsacc2004
post May 15 2017, 04:19 PM

No life without MUsic
******
Senior Member
1,860 posts

Joined: Sep 2006
From: KL


QUOTE(phyckeryz @ May 15 2017, 10:14 AM)
whats mean by #TEMP1, can be any name?
i have think to do this below but not work..
(select sum(harga) as totalA from table1 where id='a' and insert into table2 (total)values (totalA)  )
**such a mess query  cry.gif  cry.gif
*
#TEMP1 - temporary table
Eventless
post May 15 2017, 06:02 PM

Look at all my stars!!
*******
Senior Member
2,643 posts

Joined: Jan 2003
QUOTE(alien3d @ May 15 2017, 11:09 AM)
CODE

INSERT INTO `table2` ('a',(
 SELECT SUM(harga)
 FROM `table1`
 WHERE id = 'a'
 GROUP BY `a`
)
);


Dear ts.. sila rajin rajin guna  tool macam
http://sqlfiddle.com/
whistling.gif
*
CODE
GROUP BY `a`

This line of code practically does nothing. All the lines returned will contain records with 'a' as the ID. No point in grouping anything. The code below should give the same result.
CODE
insert into table2(id,total)
select 'a',sum(harga)
from table1
where id='a'


If you do need the grouping part, the code below would be better since all you need is a single change if you want the total for a different id.
CODE
insert into table2(id,total)
select id,sum(harga)
from table1
where id='a'
group by id

Eventless
post May 15 2017, 06:05 PM

Look at all my stars!!
*******
Senior Member
2,643 posts

Joined: Jan 2003
QUOTE(phyckeryz @ May 15 2017, 10:01 AM)
hi master.. i hope can ask guide from u guys here..

my issue here how to sum data in one of table field and insert into second table the total value..  sweat.gif  sweat.gif
for ur easy understand i put the sample table figure below..  icon_question.gif  icon_question.gif

Attached Image

notworthy.gif  notworthy.gif how to query master.. icon_question.gif  icon_question.gif
*
Your table2 will essentially be the same as the result of the below query.
CODE
select id,sum(harga) as total
from table1
group by id

alien3d
post May 15 2017, 07:42 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(Eventless @ May 15 2017, 06:02 PM)
CODE
GROUP BY `a`

This line of code practically does nothing. All the lines returned will contain records with 'a' as the ID. No point in grouping anything. The code below should give the same result.
CODE
insert into table2(id,total)
select 'a',sum(harga)
from table1
where id='a'


If you do need the grouping part, the code below would be better since all you need is a single change if you want the total for a different id.
CODE
insert into table2(id,total)
select id,sum(harga)
from table1
where id='a'
group by id

*
ah my mistake.. but still this question a bit odd by da way
TSphyckeryz
post May 16 2017, 10:57 AM

Getting Started
**
Junior Member
69 posts

Joined: Jan 2013
From: Malaysia

QUOTE(Eventless @ May 15 2017, 06:05 PM)
Your table2 will essentially be the same as the result of the below query.
CODE
select id,sum(harga) as total
from table1
group by id

*
CODE
insert into table2 (id,total)
values
('a', (select sum(harga) from table1 where id='a')


i used this query.. and its work like charm.. rclxm9.gif rclxm9.gif
thanks for the otherway to code the query..will keep for my reference.. notworthy.gif notworthy.gif
TSphyckeryz
post May 16 2017, 11:01 AM

Getting Started
**
Junior Member
69 posts

Joined: Jan 2013
From: Malaysia

QUOTE(alien3d @ May 15 2017, 07:42 PM)
ah my mistake..    but still  this question a bit odd by da way
*
sorry for uncleared question... sweat.gif sweat.gif

 

Change to:
| Lo-Fi Version
0.0271sec    0.84    6 queries    GZIP Disabled
Time is now: 22nd December 2025 - 03:54 AM