The Little Guy Posted January 7, 2011 Share Posted January 7, 2011 I have these two queries, I need to make this one query where the amount on each row adds up to a number. mysql> select SUM(amount), last_day(from_unixtime(the_date)) as d from purchases Where the_date> 1285891200 and member_id = 110719 group by d; +-------------+------------+ | SUM(amount) | d | +-------------+------------+ | 2059.9500 | 2010-10-31 | | 0.0000 | 2010-11-30 | | 0.0000 | 2010-12-31 | | 0.0000 | 2011-01-31 | +-------------+------------+ 4 rows in set (0.00 sec) mysql> select SUM(amount), last_day(from_unixtime(transfer_date)) as d from cash_out Where transfer_date> 1285891200 and user_id = 110719 AND descr LIKE '%purchase%' group by d; +-------------+------------+ | SUM(amount) | d | +-------------+------------+ | 6.31 | 2010-10-31 | | 83.82 | 2010-11-30 | | 116.88 | 2010-12-31 | | 12.51 | 2011-01-31 | +-------------+------------+ 4 rows in set (0.00 sec) I currently have this, but I am getting the wrong numbers: select sum(distinct c.amount) + sum(distinct p.amount) as amount, last_day(from_unixtime(the_date)) as d from purchases p join cash_out c on(member_id = user_id) where the_date > 1285891200 and transfer_date > 1285891200 and member_id = 110719 group by d; +-----------+------------+ | amount | d | +-----------+------------+ | 2205.7300 | 2010-10-31 | | 145.7800 | 2010-11-30 | | 145.7800 | 2010-12-31 | | 145.7800 | 2011-01-31 | +-----------+------------+ 4 rows in set (0.01 sec) I am looking for this result: +-----------+------------+ | amount | d | +-----------+------------+ | 2066.2600 | 2010-10-31 | | 83.82 | 2010-11-30 | | 116.88 | 2010-12-31 | | 12.51 | 2011-01-31 | +-----------+------------+ What can I do? Link to comment https://forums.phpfreaks.com/topic/223697-combine-2-queries/ Share on other sites More sharing options...
BLaZuRE Posted January 7, 2011 Share Posted January 7, 2011 SUM() is a function that calculates the sum for the COLUMN. You can do fielda + fieldb to give you a result of fieldc on rowX. You need to select the data you need where a.the_date = b.transfer_date Essentially, something like (untested): SELECT (a.amount + b.amount) FROM a,b WHERE a.the_date = b.transfer_date Link to comment https://forums.phpfreaks.com/topic/223697-combine-2-queries/#findComment-1156386 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.