MySQL不支持INTERSECT和MINUS,及其替代方法
2010-05-24 17:01
411 查看
Doing INTERSECT and MINUS in MySQL
By Carsten | October 3, 2005
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
By Carsten | October 3, 2005
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
相关文章推荐
- MySQL不支持INTERSECT和MINUS及其替代方法
- MySQL不支持INTERSECT和MINUS,及其替代方法
- MySQL不支持FULL JOIN, INTERSECT和MINUS(except)的替代方法
- 让MySql支持表情符号(MySQL中4字节utf8字符保存方法)
- MySQL实现差集(Minus)和交集(Intersect)
- MySQL中Union子句不支持order by的解决方法
- Mysql --nulls first(last)的替代方法
- ubuntu下的mysql不支持中文,修改方法;
- MySQL实现差集(Minus)和交集(Intersect)
- IE10/11不支持条件性注释后的替代方法
- MySQL随机获取数据的方法,支持大数据量
- 慎用option中的title属性,及其替代方法
- 让MySql支持Emoji表情(MySQL中4字节utf8mb4字符保存方法)
- MYSQL类型不支持原生SQL查询解决方法
- MySQL随机获取数据的方法,支持大数据量
- Linux下安装配置apache,mysql,php并支持gd,jpeg,png,freetype的方法
- MySQL各存储引擎(INNODB,MyISAM等)的区别及其启动方法
- 解决方法总结:服务器不支持 MySql 数据库,无法安装论坛程序
- MySQL各存储引擎的区别及其启动方法