如何在查询中用JOIN替换NOT IN?

时间:2015-08-10 09:45:25

标签: mysql sql

如何使用我提到的特定查询的连接来编写查询?如果有人可以帮助我,真的很棒!我正在尝试很多天但是我没有得到JOINS的预期结果,我尝试过Left,Right Join但是我仍然没有得到正确的结果,

没有加入:

select distinct SchoolId, utc_timestamp
from schools 
where schoolId not in ( select schoolId
                        from school_grades
                        where gradeId like '2abaf802-70c5-4096-a830-7e8873ab3772')
  and graphCode in ('Florida');

2 个答案:

答案 0 :(得分:1)

你应该试试这个。

SELECT DISTINCT
        s.SchoolId ,
        utc_timestamp
FROM    schools s
        INNER JOIN school_grades ON s.schoolId != schoolId
                                    AND gradeId LIKE '2abaf802-70c5-4096-a830-7e8873ab3772'
                                    AND s.graphCode IN ( 'Florida' ) ;

答案 1 :(得分:0)

在这种情况下使用EXISTS是合适的:

select distinct s.SchoolId, utc_timestamp
from schools s
where NOT EXISTS (select  *
                        from school_grades
                        where gradeId like '2abaf802-70c5-4096-a830-7e8873ab3772' AND 
                              schoolId = s.schoolId)
  and s.graphCode in ('Florida');

取决于您希望将IN替换为JOIN的原因。