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

mysql query help, help me
|
|
May 15 2017, 10:01 AM, updated 9y ago
Show posts by this member only | Post
#1
|
![]() ![]()
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.. for ur easy understand i put the sample table figure below.. ![]() |
|
|
|
|
|
May 15 2017, 10:06 AM
Show posts by this member only | Post
#2
|
![]() ![]() ![]() ![]() ![]() ![]()
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.. SELECT SUM(harga) as harga, ID FROM Table1 into #TEMP1 GROUP BY ID my issue here how to sum data in one of table field and insert into second table the total value.. for ur easy understand i put the sample table figure below.. ![]() insert into table2 (select * from #TEMP1) just for the concept . u need test it out as i`m using MSSQL and visual foxpro haha. |
|
|
May 15 2017, 10:14 AM
Show posts by this member only | Post
#3
|
![]() ![]()
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 whats mean by #TEMP1, can be any name?insert into table2 (select * from #TEMP1) just for the concept . u need test it out as i`m using MSSQL and visual foxpro haha. 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 |
|
|
May 15 2017, 10:30 AM
Show posts by this member only | Post
#4
|
![]() ![]()
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. |
|
|
May 15 2017, 10:37 AM
Show posts by this member only | Post
#5
|
![]() ![]()
Junior Member
69 posts Joined: Jan 2013 From: Malaysia |
|
|
|
May 15 2017, 11:09 AM
Show posts by this member only | Post
#6
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
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.. for ur easy understand i put the sample table figure below.. ![]() 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/ This post has been edited by alien3d: May 15 2017, 11:10 AM |
|
|
|
|
|
May 15 2017, 11:35 AM
Show posts by this member only | Post
#7
|
![]() ![]()
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/ trimas atas nasihat siffuu.. |
|
|
May 15 2017, 04:19 PM
Show posts by this member only | Post
#8
|
![]() ![]() ![]() ![]() ![]() ![]()
Senior Member
1,860 posts Joined: Sep 2006 From: KL |
|
|
|
May 15 2017, 06:02 PM
Show posts by this member only | Post
#9
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
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/ 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 |
|
|
May 15 2017, 06:05 PM
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
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.. Your table2 will essentially be the same as the result of the below query.my issue here how to sum data in one of table field and insert into second table the total value.. for ur easy understand i put the sample table figure below.. ![]() CODE select id,sum(harga) as total from table1 group by id |
|
|
May 15 2017, 07:42 PM
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
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 |
|
|
May 16 2017, 10:57 AM
|
![]() ![]()
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.. thanks for the otherway to code the query..will keep for my reference.. |
|
|
May 16 2017, 11:01 AM
|
![]() ![]()
Junior Member
69 posts Joined: Jan 2013 From: Malaysia |
|
| Change to: | 0.0194sec
0.76
6 queries
GZIP Disabled
Time is now: 22nd December 2025 - 12:04 AM |