MySQL不支持INTERSECT和MINUS及其替代方法
更新時間:2014年03月26日 17:14:14 作者:
這篇文章主要介紹了MySQL不支持INTERSECT和MINUS情況下的替代方法,需要的朋友可以參考下
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
復(fù)制代碼 代碼如下:
SELECT member_id, name FROM a
INTERSECT
SELECT member_id, name FROM b
can simply be rewritten to
復(fù)制代碼 代碼如下:
SELECT a.member_id, a.name
FROM a INNER JOIN b
USING (member_id, name)
Performing a MINUS
To transform the statement
復(fù)制代碼 代碼如下:
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:
復(fù)制代碼 代碼如下:
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:
復(fù)制代碼 代碼如下:
SELECT DISTINCT a.member_id, a.name
FROM a LEFT JOIN b USING (member_id, name)
WHERE b.member_id IS NULL
您可能感興趣的文章:
- mysql中char與varchar的區(qū)別分析
- Mysql varchar大小長度問題介紹
- mysql VARCHAR的最大長度到底是多少
- mysql 開啟慢查詢 如何打開mysql的慢查詢?nèi)罩居涗?/a>
- MySQL慢查詢查找和調(diào)優(yōu)測試
- MySQL前綴索引導(dǎo)致的慢查詢分析總結(jié)
- 深入mysql慢查詢設(shè)置的詳解
- MySQL的Replace into 與Insert into on duplicate key update真正的不同之處
- mysqlsla慢查詢分析工具使用筆記
- MySQL中把varchar類型轉(zhuǎn)為date類型方法詳解
- MySQL數(shù)據(jù)庫中把int轉(zhuǎn)化varchar引發(fā)的慢查詢
相關(guān)文章
mysql數(shù)據(jù)庫實現(xiàn)設(shè)置字段長度
這篇文章主要介紹了mysql數(shù)據(jù)庫實現(xiàn)設(shè)置字段長度,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
PHP mysqli擴(kuò)展庫 預(yù)處理技術(shù)的使用分析
本篇文章,介紹了PHP mysqli擴(kuò)展庫 預(yù)處理技術(shù)的使用分析。需要的朋友參考下2013-05-05
MySQL關(guān)于字符串中數(shù)字排序的問題分析
這篇文章主要介紹了MySQL關(guān)于字符串中數(shù)字排序的問題,結(jié)合實例形式分析了mysql按照數(shù)值排序的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
mysql 8.0.15 winx64解壓版安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了mysql 8.0.15 winx64解壓版安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02

