I need to combine these two queries and get records using the single mySQL query.
Here are my queries
select DATE_FORMAT(campaign_date, '%Y-%m' ) AS month
, sum(sms)
from table1
group
by month
it returns me, sum of all months eg
2019-05 5400
2019-06 3200
2019-07 11505
etc
I have another query which gets data in same format but from a different table.
select DATE_FORMAT(trans_date, '%Y-%m' ) AS month
, sum(camp_sms)
from table2
group
by month
2019-05 3500
2019-06 7256
2019-07 35465
etc
is it possible to combine these two query and get data same like this below:
Date sum(sms) sum(camp_sms)
2019-05 5400 3500
2019-06 3200 7256
2019-07 11505 35465
I have done this using PHP loops & array and to get the same output, but i want to do it using mySQL.
Simply use join
select t1.month, t1.total, t2.total from (
select DATE_FORMAT(campaign_date, '%Y-%m' ) AS month, sum(sms) total from table1 group by month
) t1
join (
select DATE_FORMAT(trans_date, '%Y-%m' ) AS month, sum(camp_sms) from table2 group by month
) t2 on t1.month = t2.month
如果您不想跳过表1中的任何数据,则应该类似地使用LEFT JOIN
您可以根据需要使用其他联接。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.