我有两张桌子: 具有object_id列的对象 和具有object_id(对象为object.object_id的FK)和value_type的avalues(为简单起见,我省略了其他列)。
我想选择所有没有指定类型值的对象。我的选择看起来像这样:
SELECT object_id FROM object WHERE NOT EXISTS (SELECT true FROM avalues v WHERE v.value_type = 10 and v.object_id = object_id);
有更有效的方法吗?
答案 0 :(得分:3)
NOT IN
和LEFT JOIN / IS NULL
稍微提高效率:
SELECT object_id
FROM object
WHERE object_id NOT IN
(
SELECT object_id
FROM avalues v
WHERE v.value_type = 10
)
或
SELECT o.object_id
FROM object o
LEFT JOIN
avalues v
ON v.object_id = o.object_id
AND v.value_type = 10
WHERE v.object_id IS NULL
确保您在avalues (object_id, value_type)