有什么办法可以优化这条sql语句吗?也许加入或什么?
SELECT id, name
FROM item
WHERE id NOT IN (
SELECT id
FROM itemlock
) AND id NOT IN (
SELECT id
FROM itemlog
)
谢谢
采用LEFT JOIN
SELECT a.*
FROM item a
LEFT JOIN itemLock b
ON a.ID = b.ID
LEFT JOIN itemLog c
ON a.ID = c.ID
WHERE b.ID IS NULL AND
c.ID IS NULL
您也可以使用 UNION,如下所示:
SELECT id, name FROM item
WHERE id NOT IN (
SELECT id FROM itemlock UNION SELECT id FROM itemlog
)
试试这个:
SELECT id, NAME FROM item i
LEFT JOIN (SELECT id FROM itemlock
UNION
SELECT id FROM itemlog) AS A ON i.id = A.id
WHERE A.id IS NULL;