问题
I have a query which is not working as expected
Q1:
SELECT id, name
FROM vw_x
WHERE id NOT IN (select pid from table_x)
GROUP BY id, name
Having max(c_date) > GETDATE()
Q2:
SELECT id, name
FROM vw_x
GROUP BY id, name
Having max(c_date) > GETDATE()
Q1 is not returning anything even though i know those ids are not in table_x Q2 runs correctly without NOT IN
What could be wrong with my query?
回答1:
you have a NULL value in the table
try this
SELECT id, name
FROM vw_x
WHERE id NOT IN (select pid from table_x where pid is not null)
GROUP BY id, name
Having max(c_date) > GETDATE()
or this
SELECT id, name
FROM vw_x
WHERE NOT EXISTS (select 1 from table_x where pid = vw_x.id )
GROUP BY id, name
Having max(c_date) > GETDATE()
See also Select all rows from one table that don't exist in another table
回答2:
what about using a left join?
SELECT id, name
FROM vw_x
LEFT JOIN table_x on id = pid
WHERE pid IS NULL
GROUP BY id, name
Having max(c_date) > GETDATE()
回答3:
There is another situation: the subquery may return nothing. SQL Server does not work as expected if the NOT IN clause returns a null list. I have a query like the following:
select * from table where id not in (select id from tableB where somecondition(x))
When the subquery contains a list of ids, the query will return the data as expected. But when the subquery returns nothing, the query will still return data, but then becomes stuck.
I changed the query to the following and solved the problem:
select * from table where id not in (select id from tableB where somecondition(x) **union all select 0**)
which makes sure the subquery will contain at least one number.
来源:https://stackoverflow.com/questions/2187780/sql-not-in-clause