sko*_*ine 82 postgresql subquery
我正在尝试执行此查询:
SELECT mac, creation_date
FROM logs
WHERE logs_type_id=11
AND mac NOT IN (select consols.mac from consols)
Run Code Online (Sandbox Code Playgroud)
但我没有结果.我测试了它,我知道语法有问题.在MySQL中,这样的查询非常有效.我添加了一行以确保表mac
中不存在一行consols
,但仍然没有给出任何结果.
Mar*_*ers 145
使用NOT IN时,应确保没有值为NULL:
SELECT mac, creation_date
FROM logs
WHERE logs_type_id=11
AND mac NOT IN (
SELECT mac
FROM consols
WHERE mac IS NOT NULL -- add this
)
Run Code Online (Sandbox Code Playgroud)
wil*_*ser 27
使用NOT IN时,您还应该考虑NOT EXISTS,它以静默方式处理空案例.
SELECT mac, creation_date
FROM logs lo
WHERE logs_type_id=11
AND NOT EXISTS (
SELECT *
FROM consols nx
WHERE nx.mac = lo.mac
);
Run Code Online (Sandbox Code Playgroud)
您还可以使用LEFT JOIN和IS NULL条件:
SELECT
mac,
creation_date
FROM
logs
LEFT JOIN consols ON logs.mac = consols.mac
WHERE
logs_type_id=11
AND
consols.mac IS NULL;
Run Code Online (Sandbox Code Playgroud)
"mac"列上的索引可能会提高性能.
归档时间: |
|
查看次数: |
125124 次 |
最近记录: |