如何显示两个不同的表不匹配记录?

时间:2016-11-03 11:12:45

标签: mysql

SELECT d.c_id,  
       d.total_amt,
       d.date AS pay_detail_dish, 
       w.amount, 
       w.date AS pay_detail_dishweb 
FROM  pay_detail_dish d, pay_detail_dishweb w 
//WHERE d.c_id = w.c_id 

以上代码显示所有记录但不匹配记录不显示在mysql中

1 个答案:

答案 0 :(得分:0)

不幸的是,MySQL不支持FULL OUTER JOIN,因此您必须LEFT JOIN - > UNION - > RIGHT JOIN

SELECT d.c_id,  
   d.total_amt,
   d.date AS pay_detail_dish, 
   w.amount, 
   w.date AS pay_detail_dishweb 
FROM  pay_detail_dish d
LEFT JOIN pay_detail_dishweb w ON d.c_id = w.c_id 
UNION ALL
SELECT s.c_id,  
   s.total_amt,
   s.date AS pay_detail_dish, 
   p.amount, 
   p.date AS pay_detail_dishweb 
FROM  pay_detail_dish s
RIGHT JOIN pay_detail_dishweb p ON s.c_id = p.c_id 
WHERE s.c_id is null