MySQL不支持INTERSECT和MINUS及其替代方法
更新時(shí)間: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大小長(zhǎng)度問題介紹
- mysql VARCHAR的最大長(zhǎng)度到底是多少
- mysql 開啟慢查詢 如何打開mysql的慢查詢?nèi)罩居涗?/a>
- MySQL慢查詢查找和調(diào)優(yōu)測(cè)試
- MySQL前綴索引導(dǎo)致的慢查詢分析總結(jié)
- 深入mysql慢查詢?cè)O(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索引總結(jié)----mysql索引類型以及創(chuàng)建
索引是一種特殊的文件(InnoDB數(shù)據(jù)表上的索引是表空間的一個(gè)組成部分),它們包含著對(duì)數(shù)據(jù)表里所有記錄的引用指針。這篇文章主要介紹了詳解mysql索引總結(jié)----mysql索引類型以及創(chuàng)建,有興趣的可以了解一下。2016-11-11mysql數(shù)據(jù)庫連接失敗常見問題小結(jié)
你有沒有碰到過mysql數(shù)據(jù)庫連接不上的問題呢?很多的小伙伴表示,經(jīng)常會(huì)時(shí)不時(shí)的出現(xiàn)這些問題,下面這篇文章主要給大家介紹了關(guān)于mysql數(shù)據(jù)庫連接失敗常見問題的相關(guān)資料,需要的朋友可以參考下2023-06-06MySQL?數(shù)據(jù)庫聚合查詢和聯(lián)合查詢操作
這篇文章主要介紹了MySQL?數(shù)據(jù)庫聚合查詢和聯(lián)合查詢操作,需要的朋友可以參考下2021-12-12mybatis統(tǒng)計(jì)每條SQL的執(zhí)行時(shí)間的方法示例
這篇文章主要介紹了mybatis統(tǒng)計(jì)每條SQL的執(zhí)行時(shí)間的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01mysql錯(cuò)誤處理之ERROR 1786 (HY000)
最近一直在mysql的各個(gè)版本直接徘徊,這中間遇到了各種各樣的錯(cuò)誤,將已經(jīng)處理完畢的幾個(gè)錯(cuò)誤整理了一下,分享給大家,首先我們來看看錯(cuò)誤提示 ERROR 1786 (HY000)2014-07-07MySQL按年/月/周/日/小時(shí)分組查詢、排序、limit及判空用法實(shí)例
我們?cè)谟肕ysql抽取數(shù)據(jù)時(shí)候,經(jīng)常需要按照天、周、月等不同的粒度對(duì)數(shù)據(jù)進(jìn)行分組統(tǒng)計(jì),下面這篇文章主要給大家介紹了關(guān)于MySQL按年/月/周/日/小時(shí)分組查詢、排序、limit及判空用法的相關(guān)資料,需要的朋友可以參考下2023-03-03MySQL命令行導(dǎo)出與導(dǎo)入數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了利用命令行MySQL導(dǎo)出數(shù)據(jù)庫與導(dǎo)入數(shù)據(jù)庫的例子,感興趣的小伙伴們可以參考一下2016-06-06