由于我們無(wú)法在 MySQL 中使用 MINUS 查詢,因此我們將使用 LEFT JOIN 來(lái)模擬 MINUS 查詢。可以借助以下示例來(lái)理解:
示例
在此示例中,我們有兩個(gè)表,即 Student_detail 和 Student_info,其內(nèi)容如下數(shù)據(jù) –
mysql> Select * from Student_detail; +-----------+---------+------------+------------+ | studentid | Name | Address | Subject | +-----------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | | 150 | Rajesh | Jaipur | Yoga | | 160 | Pradeep | Kochi | Hindi | +-----------+---------+------------+------------+ 7 rows in set (0.00 sec) mysql> Select * from Student_info; +-----------+-----------+------------+-------------+ | studentid | Name | Address | Subject | +-----------+-----------+------------+-------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | | 165 | Abhimanyu | Calcutta | Electronics | +-----------+-----------+------------+-------------+ 6 rows in set (0.00 sec)
登錄后復(fù)制
現(xiàn)在,以下使用 LEFT JOIN 的查詢將模擬 MINUS 以返回 Student_info 中的“studentid”值,但不返回 Student_detail 表中的值。
mysql> SELECT studentid from student_info LEFT JOIN Student_detail USING(studentid) WHERE student_detail.studentid IS NULL; +-----------+ | studentid | +-----------+ | 165 | +-----------+ 1 row in set (0.07 sec)
登錄后復(fù)制
現(xiàn)在,以下查詢將為我們提供與上述查詢相反的結(jié)果,即它將返回 Student_detail 中的“studentid”值,但不會(huì)返回 Student_info 表中的值。
mysql> SELECT studentid from student_detail LEFT JOIN Student_info USING(studentid) WHERE student_info.studentid IS NULL; +-----------+ | studentid | +-----------+ | 150 | | 160 | +-----------+ 2 rows in set (0.00 sec)
登錄后復(fù)制
以上就是如何使用MySQL LEFT JOIN 來(lái)模擬MySQL MINUS 查詢?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!