SQL Where ID equals ID but can be null

吃可爱长大的小学妹 提交于 2019-12-13 19:27:30

问题


I'm trying to fetch some data from an db. I've got an Taak table with an possible idPartij column. Possible, because it can be an real idPartij, but can also be null.

The query I've got:

SELECT T.idTaak,
       T.Taaktype,
       P.Partijnaam,
       T.Naar,
       T.UltimatumDatum,
       T.Opmerking,
       T.Status,
       T.Prioriteit
FROM Taak AS T,
     Partij AS P
WHERE T.idPartij = P.idPartij
ORDER BY idTaak DESC

This is working fine when I've got an id in T.idPartij, but as mentioned earlier, that id can be null. And when that is the case, the row won't be in the result.

Only problem: When I remove the where clause, I get the rows a lot ( because the Partij table isn't filtered anymore...

I'm an total noob at SQL, so i can't think of an way to "fix" this problem. Only thing i can think of is creating 2 query's, but i don't think that is an nice way to do...


回答1:


Since you are trying to link two tables, you should use a LEFT OUTER JOIN instead:

SELECT T.idTaak,
       T.Taaktype,
       P.Partijnaam,
       T.Naar,
       T.UltimatumDatum,
       T.Opmerking,
       T.Status,
       T.Prioriteit
FROM Taak AS T LEFT OUTER JOIN Partij AS P
   ON T.idPartij = P.idPartij
ORDER BY idTaak DESC

If you don't want to include the NULL values use an INNER JOIN instead.

Note that you should not use these old-style-joins it's a bad habit to kick.


According to the NULL issue:

You cannot use = NULL or <> NULL because NULL is not equal or unequal to anything. NULL means unknown.

So use IS NULL or IS NOT NULL:

WHERE T.idPartij IS NULL OR T.idPartij = P.idPartij

From MSDN

A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown.




回答2:


You didn't specify a database platform, so it is safe to use ANSI syntax.

Do a coalesce to compare P.idPartij when T.idPartij is null:

where P.idPartij = coalesce(T.idPartij, P.idPartij)

It will match all T rows if T.idPartij is null.

If you want to return no T row when idPartij is null, use a left outer join:

from   Taak t
left
outer
join   Partij p
on     t.idPartij = p.idPartij



回答3:


Instead of inner join you need to use left join to get the missing rows.

WHERE T.idPartij = P.idPartij OR T.idPartij IS NULL



回答4:


If you want to join the tables so that (a) non-null equivalent Ids match, and (b) null Ids match, then you're query should look like:

SELECT T.idTaak,
       T.Taaktype,
       P.Partijnaam,
       T.Naar,
       T.UltimatumDatum,
       T.Opmerking,
       T.Status,
       T.Prioriteit
FROM Taak AS T INNER JOIN Partij AS P
   ON T.idPartij = P.idPartij OR T.idPartij IS NULL AND P.idPartij IS NULL
ORDER BY idTaak DESC


来源:https://stackoverflow.com/questions/24533406/sql-where-id-equals-id-but-can-be-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!