MySQL不支持INTERSECT和MINUS,及其替代方法
2014-05-07 12:20
344 查看
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
can simply be rewritten to
Performing a MINUS
To transform the statement
into something that MySQL can process, we can utilize subqueries (available from MySQL 4.1 onward). The easy-to-understand transformation is:
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:
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 SELECT中LIMIT时EXPLAIN估算ROWS不准确的替代方法
- 让MySql支持Emoji表情(MySQL中4字节utf8mb4字符保存方法)
- MySQL实现差集(Minus)和交集(Intersect)
- MySQL基本分页查询方法及其优化
- mysql字符串不支持emoj表情的处理方法
- mysql的函数concat的,drop语句使用、列结构、所有变量、VERSION、CURRENT_DATE、支持selec+/ USER()支持详细时间计算方法
- Mybatis Generator的model生成中文注释,支持oracle和mysql(通过实现CommentGenerator接口的方法来实现)
- MySQL中ROLLUP的替代方法
- 让MySql支持表情符号(MySQL中4字节utf8字符保存方法)
- 让MySql支持Emoji表情(MySQL中4字节utf8字符保存方法)
- uniqueIdentifier在ios7不支持后的替代方法
- MySQL中Union子句不支持order by的解决方法
- Mysql --nulls first(last)的替代方法
- MySQL各存储引擎的区别及其启动方法
- mysql索引适应方法及其性能优化见解
- MYSQL 无缝替代 SQL Server row_number()方法