您好,欢迎访问一九零五行业门户网

如何使用MySQL LEFT JOIN 来模拟MySQL MINUS 查询?

由于我们无法在 mysql 中使用 minus 查询,因此我们将使用 left join 来模拟 minus 查询。可以借助以下示例来理解:
示例在此示例中,我们有两个表,即 student_detail 和 student_info,其内容如下数据 -
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)
现在,以下使用 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)
现在,以下查询将为我们提供与上述查询相反的结果,即它将返回 student_detail 中的“studentid”值,但不会返回 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)
以上就是如何使用mysql left join 来模拟mysql minus 查询?的详细内容。
其它类似信息

推荐信息