技术文章分类(180)

技术随笔(11)

多表级联,实现功能强大,标题所不能描述,下面有图片

表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

 

posted @ 2014-03-25 11:12  坤哥MartinLi  阅读(128)  评论(0编辑  收藏  举报