SELECT * FROM (SELECT paymentTop, salesTop, t1.id, t1.sno, t1.fadi, t1.sal_date, t2.pno, t2.pay_date FROM ( SELECT id,sno,fadi,sal_date, SUM(sales) AS salesTop FROM table1 WHERE id='11' GROUP BY sno ) AS t1 LEFT JOIN ( SELECT id, pno, fadi, pay_date SUM(payment) AS paymentTop FROM table2 WHERE id='11' GROUP BY pno ) AS t2 ON t1.id = t2.id ) AS t3 GROUP BY sno, pno ORDER BY sal_date, pay_date
t1 (table 1) t2 (table 2) +-------------------------------------+ +---------------------------------------+ | id | sal_date | no | sales | | id | pay_date | no | payment | +-------------------------------------+ +---------------------------------------+ | 11 | 20/02/2018 | 006089 | 160.80| | 11 | 15/03/2018 | 01815 | 93.85 | +-------------------------------------+ +---------------------------------------+ | 11 | 13/03/2018 | 008834 | 219.60| | 11 | 25/03/2018 | 07734 | 350,70| +-------------------------------------+ +---------------------------------------+ | 11 | 13/03/2018 | 008834 | 143.81| +-------------------------------------+ | 11 | 21/03/2018 | 010257 | 51.00| +-------------------------------------+ | 11 | 21/03/2018 | 010257 | 553.21| +-------------------------------------+ the output I want +------------------------------------------------+ | id | date | no | sales | payment | +------------------------------------------------+ | id |20/02/2018 | 006089 | 160.80| | +------------------------------------------------+ | id |13/03/2018 | 008834 | 363.41| | +------------------------------------------------+ | id |15/03/2018 | 01815 | | 93.85 | +------------------------------------------------+ | id |21/03/2018 | 010257 | 604.21| | +------------------------------------------------+ | id |25/03/2018 | 07734 | | 350,70 | +------------------------------------------------+ query output +----------------------------------------+ | date | no | sales | payment | +----------------------------------------+ |20/02/2018 |006089 | 160.80| 93.85 | +----------------------------------------+ |20/02/2018 |07734 | 160.80| 350,70 | +----------------------------------------+ |13/03/2018 |008834 | 219.60| 93.85 | +----------------------------------------+ |15/03/2018 |07734 | 219.60| 350,70 | +----------------------------------------+ |21/03/2018 |010257 | 553.21| 93.85 | +----------------------------------------+ |25/03/2018 |07734 | 553.21| 350,70 | +----------------------------------------+
答案 0 :(得分:0)
尝试在两个表之间左连接
select t1.id ,t1.sal_date,t1.no,t1.sales,t2.payment
left join t2
on t1.id=t2.id
and t1.no=t2.no