为什么我的查询没有按预期工作?
Why is my query not working like expected?
我有一个查询,我试图在其中获取不在另一个 table 中的特定值,但是当我 运行 查询时,我没有得到任何结果。这就是我所看到的。请让我知道出了什么问题。
原始查询:Returns 399 行
USE TFW_DEV
SELECT
unitid, unitnumber
FROM
Units
WHERE
createdon BETWEEN '2016-11-01 00:00:00' AND '2016-11-03 23:59:59'
ORDER BY
unitnumber
查询以查看 table 共享数据的位置:Returns 50 行
USE TFW_DEV
SELECT
unitid, unitnumber
FROM
Units
WHERE
createdon BETWEEN '2016-11-01 00:00:00' AND '2016-11-03 23:59:59'
AND unitid IN
(
SELECT
unitid
FROM
meteraudit
)
ORDER BY
unitnumber
带有 NOT IN 的最终查询:Returns 0 行
USE TFW_DEV
SELECT
unitid, unitnumber
FROM
Units
WHERE
createdon BETWEEN '2016-11-01 00:00:00' AND '2016-11-03 23:59:59'
AND unitid NOT IN
(
SELECT
unitid
FROM
meteraudit
)
ORDER BY
unitnumber
我希望最终查询 return 349 行正确吗?如果不是,我做错了什么?谢谢,
当 Sub-Query
返回 NULL
值时,NOT IN
失败
使用NOT EXISTS
SELECT unitid,
unitnumber
FROM units U
WHERE createdon BETWEEN '2016-11-01 00:00:00' AND '2016-11-03 23:59:59'
AND NOT EXISTS (SELECT 1
FROM meteraudit M
WHERE U.unitid = M.unitid)
ORDER BY unitnumber
我有一个查询,我试图在其中获取不在另一个 table 中的特定值,但是当我 运行 查询时,我没有得到任何结果。这就是我所看到的。请让我知道出了什么问题。
原始查询:Returns 399 行
USE TFW_DEV
SELECT
unitid, unitnumber
FROM
Units
WHERE
createdon BETWEEN '2016-11-01 00:00:00' AND '2016-11-03 23:59:59'
ORDER BY
unitnumber
查询以查看 table 共享数据的位置:Returns 50 行
USE TFW_DEV
SELECT
unitid, unitnumber
FROM
Units
WHERE
createdon BETWEEN '2016-11-01 00:00:00' AND '2016-11-03 23:59:59'
AND unitid IN
(
SELECT
unitid
FROM
meteraudit
)
ORDER BY
unitnumber
带有 NOT IN 的最终查询:Returns 0 行
USE TFW_DEV
SELECT
unitid, unitnumber
FROM
Units
WHERE
createdon BETWEEN '2016-11-01 00:00:00' AND '2016-11-03 23:59:59'
AND unitid NOT IN
(
SELECT
unitid
FROM
meteraudit
)
ORDER BY
unitnumber
我希望最终查询 return 349 行正确吗?如果不是,我做错了什么?谢谢,
Sub-Query
返回 NULL
值时,NOT IN
失败
使用NOT EXISTS
SELECT unitid,
unitnumber
FROM units U
WHERE createdon BETWEEN '2016-11-01 00:00:00' AND '2016-11-03 23:59:59'
AND NOT EXISTS (SELECT 1
FROM meteraudit M
WHERE U.unitid = M.unitid)
ORDER BY unitnumber