如何将两个查询合二为一,并在单个查询中获得结果?

时间:2017-07-23 12:34:58

标签: mysql mysqli

SELECT SUM(commission) as regularincome FROM `tbl_member_commission` where mem_id=2 AND MONTH(cdate) = MONTH(CURRENT_DATE())

SELECT SUM(commission) as crowdfund FROM `tbl_member_comm_month` where mem_id=2 AND MONTH(cdate) = MONTH(CURRENT_DATE())

注意: - 两个表都具有相同的列名:commission,mem_id,cdate

1 个答案:

答案 0 :(得分:0)

如果这是每个子查询中的唯一记录,则可以使用CROSS JOIN

 select a.regularincome, b.crowdfund
 FROM
    (SELECT SUM(commission) as regularincome FROM `tbl_member_commission` where mem_id=2 AND MONTH(cdate) = MONTH(CURRENT_DATE())) as a
    cross join 
    (SELECT SUM(commission) as crowdfund FROM `tbl_member_comm_month` where mem_id=2 AND MONTH(cdate) = MONTH(CURRENT_DATE())) as b