[英]How to compare 2 columns from different table
I'm trying to have a clausule where what compare a column from table A with a column from table B but the result dont bring nothing.我试图有一个条款,将表 A 中的列与表 B 中的列进行比较,但结果没有带来任何结果。 Am I doing something wrong?难道我做错了什么?
My query>我的查询>
SELECT EMP, COD, VEV, SEQ, TIP, NUM, EMI, VEN, DTR, GRU, PRO, QTD, PVE, TOT, FAT, COM, COO, REF, VAR, hcli.NOM as "VENDEDOR", hemp.enc
FROM hcov
INNER JOIN hcli ON hcov.COD = hcli.COD
INNER JOIN hemp ON hemp.cod = hcov.emp
WHERE (hcli.NOM NOT LIKE '%COOPERATIVA%')
AND (DTR >= date '2020-01-01')
AND (COD <> 24545 OR COD <> 10368 OR COD <> 13149 OR COD <> 10448 OR COD <> 11041 OR COD <> 30610 OR COD <> 6834)
AND (GRU <> 266 OR GRU <> 269 OR GRU <> 272)
AND ( (VEV <> 37125) AND (COO <> 987209 OR COO <> 23631 OR COO <> 927500 OR COO <> 22763 OR COO <> 38736 ) )
AND (hemp.enc <> hcov.cod)
AND (hemp.enc <> hcov.cod) -> aparently this line is not working as should AND (hemp.enc <> hcov.cod) -> 显然这条线没有正常工作
The result is totally blank but both column is different.. so it should bring结果完全是空白,但两列都不同..所以它应该带来
The issue is because you are comparing values where (at least one of them) is NULL.问题是因为您正在比较(至少其中一个)为 NULL 的值。
NULL is not considered a value - it is considered 'unknown'. NULL 不被视为一个值 - 它被视为“未知”。 It could be the value you're checking against, or it may not.它可能是您要检查的值,也可能不是。 Therefore, checking if anything = NULL
results in a NULL answer - the answer is also unknown.因此,检查是否有任何内容= NULL
会导致 NULL 答案 - 答案也是未知的。
Note that even checking if NULL = NULL
returns NULL.请注意,即使检查NULL = NULL
返回 NULL。 It may be that the first value is 1 and the second 1 too, therefore it would be true, Or it could be that the second value is 2. therefore it's false, As such, checking if NULL = NULL
results in NULL.可能第一个值为 1,第二个值为 1,因此为真,或者第二个值为 2。因此为假,因此检查NULL = NULL
导致 Z6C4E226B4D4795D518AB341B0824EC29Z
You can check if something IS NULL - what this is essentially asking 'whether the value is unknown'.您可以检查某些东西是否为 NULL - 这本质上是在询问“值是否未知”。 eg,例如,
IF @a = NULL
results in NULL然后检查IF @a = NULL
结果为 NULLIF @a IS NULL
results in true同时检查IF @a IS NULL
结果为真In other words, you need to work out how you want the process to work if hemp.enc
is NULL and/or hcov.cod
is NULL.换句话说,如果hemp.enc
是 NULL 和/或hcov.cod
是 NULL,您需要弄清楚您希望该过程如何工作。
For example, you may change the line in the WHERE clause to例如,您可以将 WHERE 子句中的行更改为
AND ((hemp.enc <> hcov.cod)
OR (hemp.enc IS NULL AND hcov.cod IS NOT NULL)
OR (hemp.enc IS NOT NULL AND hcov.cod IS NULL)
)
Here is a db<>fiddle with some examples of results of checking vs NULLs.这是一个db<>fiddle ,其中包含一些检查与 NULL 的结果示例。 Note that only the explicit IS NULL
and IS NOT NULL
actually return results.请注意,只有显式IS NULL
和IS NOT NULL
实际返回结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.