多表级联,实现功能强大,标题所不能描述,下面有图片
表brand0:
表brand1:
想要结合表brand0和表brand1,得到如下结果:
解决办法:
法1、
SELECT brand0.brand_id ,brand0.clickCount,brand1.saleCount FROM brand0 LEFT JOIN brand1 ON brand0.brand_id=brand1.brand_id UNION SELECT brand1.brand_id ,brand0.clickCount,brand1.saleCount FROM brand0 RIGHT JOIN brand1 ON brand1.brand_id=brand0.brand_id
法2(推荐使用这种,容易)、
SELECT brand_id, SUM(clickCount) AS clickCount, SUM(saleCount ) AS saleCount FROM ( SELECT brand_id,clickCount, 0 saleCount FROM brand0 UNION ALL SELECT brand_id,0 clickCount, saleCount FROM brand1) ta GROUP BY ta.brand_id