如何在MySQL中实现此查询?

时间:2011-02-02 16:37:26

标签: mysql database

我的查询与此非常相似。

(SELECT emailid FROM usereducation WHERE presfuncarea = '$funcarea') 
intersect
(SELECT emailid FROM userprofession WHERE totexpyear >= '$minexp')

由于MySQL不支持交叉,我必须找到正确的解决方案。

3 个答案:

答案 0 :(得分:1)

这应该做的工作:

SELECT p.emailid
FROM usereducation e JOIN userprofession p ON p.emailid = e.emailid
WHERE e.presfuncarea = '$funcarea'
AND p.totexpyear >= '$minexp'

答案 1 :(得分:0)

MySQL支持EXISTS。这应该有效:

SELECT UE.emailid
FROM usereducation AS UE
WHERE UE.presfuncarea = '$funcarea'
  AND NOT EXISTS
    (
    SELECT * FROM userprofession AS UP
    WHERE UE.emailid = UP.emailID AND UP.totexpyear >= '$minexp'
    )

答案 2 :(得分:0)

来自:http://www.bitbybit.dk/carsten/blog/?p=71

  

INTERSECT只是一个内连接,我们将一个表的元组与另一个表的元组进行比较,并选择那些同时删除重复项的表。所以

SELECT member_id, name FROM a
INTERSECT
SELECT member_id, name FROM b

可以简单地重写为

SELECT a.member_id, a.name
FROM a INNER JOIN b
USING (member_id, name)