mysql中实现并集、交集、差集、内联结、外联结、3张以上表的联结、交叉联结
2018-02-18 16:20
393 查看
一、mysql中实现并集
1.union函数
1.1
mysql> select product_id,product_name from product union select product_id,product_name from product2;
1.2order by只能使用一次,而且在最后使用
mysql> select product_id,product_name from product where product_type='厨房用具' union select product_id,product_name from product2 where product_type='厨房用具' order by product_id;
1.union all函数
mysql> select product_id,product_name from product union all select product_id,product_name from product2;
二、mysql中实现交集
1.INTERSECT函数无法在MySQL中使用,只能在orcal中使用
2.利用in实现交集
mysql> select product_id,product_name from product where product_id in(select product_id from product2);
三、mysql中实现差集
1.EXCEPT函数无法在MySQL中使用,只能在orcal中使用
2.利用not in实现差集
mysql> select product_id,product_name from product where product_id not in(select product_id from product2);
四、mysql中实现内联结
![](https://img-blog.csdn.net/20180218185629992?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc4MTU1OTY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![](https://img-blog.csdn.net/2018021818542640?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc4MTU1OTY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
内联结和WHERE子句结合使用
![](https://img-blog.csdn.net/2018021818545027?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc4MTU1OTY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
五、mysql中实现外联结
![](https://img-blog.csdn.net/20180218185656681?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc4MTU1OTY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![](https://img-blog.csdn.net/20180218185541207?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc4MTU1OTY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![](https://img-blog.csdn.net/20180218185725218?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc4MTU1OTY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
六、3张以上表的联结
mysql> select sp.shop_id,sp.shop_name,sp.product_id,p.product_name,p.sale_price,ip.inventory_quantity from ShopProduct as sp INNER JOIN product as p ON sp.product_id = p.product_id INNER JOIN InventoryProduct as ip ON sp.product_id = ip.product_id where ip.inventory_id='p001';
七、交叉联结
交叉联结的集合运算符是 笛卡儿积
mysql> select sp.shop_id,sp.shop_name,sp.product_id,p.product_name from ShopProduct as sp CROSS JOIN product as p;
1.union函数
1.1
mysql> select product_id,product_name from product union select product_id,product_name from product2;
1.2order by只能使用一次,而且在最后使用
mysql> select product_id,product_name from product where product_type='厨房用具' union select product_id,product_name from product2 where product_type='厨房用具' order by product_id;
1.union all函数
mysql> select product_id,product_name from product union all select product_id,product_name from product2;
二、mysql中实现交集
1.INTERSECT函数无法在MySQL中使用,只能在orcal中使用
2.利用in实现交集
mysql> select product_id,product_name from product where product_id in(select product_id from product2);
三、mysql中实现差集
1.EXCEPT函数无法在MySQL中使用,只能在orcal中使用
2.利用not in实现差集
mysql> select product_id,product_name from product where product_id not in(select product_id from product2);
四、mysql中实现内联结
内联结和WHERE子句结合使用
五、mysql中实现外联结
六、3张以上表的联结
mysql> select sp.shop_id,sp.shop_name,sp.product_id,p.product_name,p.sale_price,ip.inventory_quantity from ShopProduct as sp INNER JOIN product as p ON sp.product_id = p.product_id INNER JOIN InventoryProduct as ip ON sp.product_id = ip.product_id where ip.inventory_id='p001';
七、交叉联结
交叉联结的集合运算符是 笛卡儿积
mysql> select sp.shop_id,sp.shop_name,sp.product_id,p.product_name from ShopProduct as sp CROSS JOIN product as p;
相关文章推荐
- Set数据结构实现数组的交集、并集、差集
- Python 两个列表的差集、并集和交集实现代码
- MySQL 的并集、交集和差集
- Java实现数组的交集、并集、差集
- mysql中交集,并集,差集,左连接,右连接
- ES6使用Set数据结构实现数组的交集、并集、差集功能示例
- Python 两个列表的差集、并集和交集实现代码
- 两个数组并集 交集 差集的算法思想与实现
- 交集、并集、差集实现代码
- mysql取差集、交集、并集
- MySQL 的并集、交集和差集 交集和差集类似oracle中minus的用法
- 递增有序的顺序表表示集合,求解两个集合的交集 并集 差集(c语言实现)
- MySQL实现差集(Minus)和交集(Intersect)测试报告
- List实现:交集,并集,差集
- 重载运算符“+”、“*”、“-”实现集合的并集、交集、差集运算
- JS实现并集,交集和差集
- 求两个数组的交集、并集和差集算法分析与实现(转自http://blog.sina.com.cn/s/blog_616e189f0100mrdn.html)
- MySQL 的并集、交集和差集
- mysql中交集,并集,差集,左连接,右连接