Mysql Union实例_MySQL
程序员文章站
2022-04-20 18:41:15
...
前提,是每个查询结果集的列数要一样,类型要一样
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 小心urldecode引发的SQL注入漏洞_PHP
下一篇: nginx源码学习资源
推荐阅读
-
分享一个Mysql主从同步记录
-
在 CentOS 7 中安装 MySQL 8 的教程详解
-
MySQL基本简单操作
-
Linux连接mysql报错:Access denied for user ‘root’@‘localhost’(using password: YES)的解决
-
CentOS7下Mysql5.7安装
-
MySQL存储表情时报错:java.sql.SQLException: Incorrect string value:‘\xF0\x9F\x92\xA9\x0D
-
Oracle与MySQL的分页查询sql语句格式实例讲解
-
MySQL中的视图使用详解
-
php+mysql数据库实现无限分类的方法
-
MySQL之游标的使用方法