Doing INTERSECT and MINUS in MySQL
Doing an INTERSECT
An INTERSECT is simply an inner join where we compare the tuples of one table with those of the other, and select those that appear in both while weeding out duplicates. So
复制代码 代码如下:
SELECT member_id, name FROM a
INTERSECT
SELECT member_id, name FROM b
can simply be rewritten to
复制代码 代码如下:
SELECT a.member_id, a.name
FROM a INNER JOIN b
USING (member_id, name)
Performing a MINUS
To transform the statement
复制代码 代码如下:
SELECT member_id, name FROM a
MINUS
SELECT member_id, name FROM b
into something that MySQL can process, we can utilize subqueries (available from MySQL 4.1 onward). The easy-to-understand transformation is:
复制代码 代码如下:
SELECT DISTINCT member_id, name
FROM a
WHERE (member_id, name) NOT IN
(SELECT member_id, name FROM table2);
Of course, to any long-time MySQL user, this is immediately obvious as the classical use-left-join-to-find-what-isn't-in-the-other-table:
复制代码 代码如下:
SELECT DISTINCT a.member_id, a.name
FROM a LEFT JOIN b USING (member_id, name)
WHERE b.member_id IS NULL
Doing an INTERSECT
An INTERSECT is simply an inner join where we compare the tuples of one table with those of the other, and select those that appear in both while weeding out duplicates. So
复制代码 代码如下:
SELECT member_id, name FROM a
INTERSECT
SELECT member_id, name FROM b
can simply be rewritten to
复制代码 代码如下:
SELECT a.member_id, a.name
FROM a INNER JOIN b
USING (member_id, name)
Performing a MINUS
To transform the statement
复制代码 代码如下:
SELECT member_id, name FROM a
MINUS
SELECT member_id, name FROM b
into something that MySQL can process, we can utilize subqueries (available from MySQL 4.1 onward). The easy-to-understand transformation is:
复制代码 代码如下:
SELECT DISTINCT member_id, name
FROM a
WHERE (member_id, name) NOT IN
(SELECT member_id, name FROM table2);
Of course, to any long-time MySQL user, this is immediately obvious as the classical use-left-join-to-find-what-isn't-in-the-other-table:
复制代码 代码如下:
SELECT DISTINCT a.member_id, a.name
FROM a LEFT JOIN b USING (member_id, name)
WHERE b.member_id IS NULL
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“MySQL不支持INTERSECT和MINUS及其替代方法”评论...
更新日志
2024年07月05日
2024年07月05日
- 中唱唱片群星《好歌珍藏-激情年代》2CD【WAV】
- 王韵婵.1996-需要【现代派】【WAV+CUE】
- 群星.2024-你就在我身边电影原声专辑【奔跑怪物】【FLAC分轨】
- 何映达.1990-孤独地拥有你【蓝与白】【WAV+CUE】
- dnf新鲜冒险奇闻有什么用
- dnf无色套是哪几件
- dnf旭旭宝宝红眼装备是什么搭配
- 陈思安2005-我无醉[华特][WAV+CUE]
- 张美玲2013-我知道你也爱我[南方][WAV+CUE]
- 萨顶顶-天籁魅声[2CD][WAV+CUE]
- 群星.1987-国语老歌龙虎榜【丽风】【WAV+CUE】
- 郑敬基.1988-夜颜(2006新世纪复刻版)【TheForwardThinker】【WAV+CUE】
- 曹格.2016-我们是朋友【滚石】【WAV+CUE】
- 贵族音乐 / 许惠钧《钢琴沉睡 睡眠赫兹 放松轻听白噪音》[320K/MP3][120.97MB]
- 贵族音乐 / 许惠钧《钢琴沉睡 睡眠赫兹 放松轻听白噪音》[FLAC/分轨][392.92MB]