开发者

How to implement this query in MySQL?

开发者 https://www.devze.com 2023-02-08 07:54 出处:网络
My query is very similar to this. (SELECT emailid FROM usereducation WHERE presfuncarea = \'$funcarea\')

My query is very similar to this.

(SELECT emailid FROM usereducation WHERE presfuncarea = '$funcarea') 
intersect
(开发者_运维技巧SELECT emailid FROM userprofession WHERE totexpyear >= '$minexp')

Since MySQL does not support intersect, I have to find the right solution.


This should do the job :

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


MySQL supports EXISTS. This should work:

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'
    )


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

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号