首頁 > 資料庫 > mysql教程 > Mysql Union实例_MySQL

Mysql Union实例_MySQL

WBOY
發布: 2016-06-01 13:12:20
原創
1054 人瀏覽過
前提,是每个查询结果集的列数要一样,类型要一样
登入後複製
SELECT g.goods_id FROM ( SELECT gr.`GOODS_ID`,COUNT(gr.`GOODS_ID`) num FROM rkgl_goods_repertory gr JOIN rkgl_repertory r ON r.`ID`=gr.`REPERTORY_ID` JOIN spgl_goods g ON g.`ID`=gr.`GOODS_ID` JOIN spgl_brand b ON b.`ID`=g.`BRAND_ID` JOIN spgl_goods_type gt ON gt.`ID`=g.`GOODS_TYPE_ID` WHERE r.`USER_ID`=36 AND g.`IS_DELETE`=FALSE AND gt.`IS_DELETE`=FALSE AND b.`IS_DELETE`=FALSE AND gr.`NUM`=0 GROUP BY gr.`GOODS_ID`) g WHERE g.num>1 UNION SELECT g.goods_id FROM ( SELECT gr.`GOODS_ID`,COUNT(gr.`GOODS_ID`) num FROM rkgl_goods_repertory gr JOIN rkgl_repertory r ON r.`ID`=gr.`REPERTORY_ID` JOIN spgl_goods g ON g.`ID`=gr.`GOODS_ID` JOIN spgl_brand b ON b.`ID`=g.`BRAND_ID` JOIN spgl_goods_type gt ON gt.`ID`=g.`GOODS_TYPE_ID` WHERE r.`USER_ID`=36 AND g.`IS_DELETE`=FALSE AND gt.`IS_DELETE`=FALSE AND b.`IS_DELETE`=FALSE AND gr.`NUM`=0 GROUP BY gr.`GOODS_ID`) g WHERE g.num=1
登入後複製

简单的实例就是

select * from a union select * from b
登入後複製


或者

(select * from a join b on a.id=b.a_id ) c union select * from d
登入後複製


还有一点就是UNION是过滤相同结果集求存不同结果集,而UNION ALL是全部累加一起。

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板