SQL查询结果错误

时间:2017-02-01 14:01:43

标签: sql sql-server

我有一个视图(A),在运行以下查询时包含9行:

SELECT *
FROM A
WHERE card_num_full is null OR card_num_full LIKE ''

表B不包含这9行,那么为什么在运行以下查询时,我仍然得到9行?

SELECT *
FROM A
WHERE card_num_full is null OR card_num_full LIKE ''
      AND field_1 IN   
        (SELECT field_1
         FROM B)

2 个答案:

答案 0 :(得分:2)

or

括起来
SELECT * 
FROM A 
WHERE (card_num_full is null 
    or card_num_full like '')
and field_1 in (select field_1 from B)

答案 1 :(得分:1)

您可以使用ISNULL()功能,但不需要括号。

SELECT * 
FROM A WHERE isnull(card_num_full,'') like ''  
    and field_1 in
        (select field_1 from B)