由于我们无法在mysql中使用intersect查询,因此我们将使用exist运算符来模拟intersect查询。可以通过以下示例来理解 -
示例在此示例中,我们有两个表,即 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)
现在,使用带有 where 子句的 exist 运算符的以下查询将模拟 intersect 以返回两个表中都存在的“studentid”、姓名、地址(其中名称不是“yashpal”) -
mysql>select student_detail.studentid,student_detail.name, student_detail.address from student_detail where student_detail.studentid >100 and exists (select * from student_info where student_info.name <> 'yashpal' and student_info.studentid = student_detail.studentid and student_info.name = student_detail.name);+-----------+--------+------------+| studentid | name | address |+-----------+--------+------------+| 105 | gaurav | chandigarh || 130 | ram | jhansi || 132 | shyam | chandigarh || 133 | mohan | delhi |+-----------+--------+------------+4 rows in set (0.00 sec)
以上就是我们如何模拟返回多个表达式的 mysql intersect 查询?的详细内容。