还显示具有NULL值的行

时间:2016-02-22 06:24:57

标签: mysql sql

当我在没有where子句的情况下点击下面的代码时,它会显示具有NULL值的行,但是当我将where子句放在下面时,则没有NULL值的行。

我想要带有NULL值的行和where子句。

SELECT * 
FROM project_category as a 
LEFT OUTER JOIN  project_estimate_detail  as b 
  ON a.id = b.project_cat_id 
where b.project_cat_id not in ('21','22','2')

3 个答案:

答案 0 :(得分:2)

试试这个:

SELECT * 
FROM project_category as a 
LEFT OUTER JOIN  project_estimate_detail  as b 
   ON a.id = b.project_cat_id 
where (b.project_cat_id IS NULL) OR (b.project_cat_id not in ('21','22','2'))

b.project_cat_idNULL时,NOT IN评估为NULL。因此,您必须使用NULL表达式明确检查IS NULL

答案 1 :(得分:1)

SELECT *
FROM   project_category AS a
       LEFT OUTER JOIN project_estimate_detail AS b
                    ON a.id = b.project_cat_id
WHERE  b.project_cat_id NOT IN ( '21', '22', '2' )
       AND b.project_cat_id IS NULL 

答案 2 :(得分:1)

您加入a.id = b.project_cat_id,然后过滤内部b.project_cat_id,这将删除所有NULL。

只需更改WHERE - 条件即可使用外部表中的加入列:

SELECT * 
FROM project_category as a 
LEFT OUTER JOIN  project_estimate_detail  as b 
  ON a.id = b.project_cat_id 
WHERE a.id not in ('21','22','2')