当我在没有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')
答案 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_id
为NULL
时,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')