1

有什么办法可以优化这条sql语句吗?也许加入或什么?

SELECT id, name
FROM item
WHERE id NOT IN (
    SELECT id 
FROM itemlock
) AND id NOT IN (
SELECT id
    FROM itemlog
)

谢谢

4

3 回答 3

3

采用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
于 2012-12-14T03:28:33.117 回答
0

您也可以使用 UNION,如下所示:

SELECT id, name FROM item 
WHERE id NOT IN (
   SELECT id  FROM itemlock UNION SELECT id FROM itemlog
)
于 2012-12-14T03:52:21.523 回答
0

试试这个:

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;
于 2012-12-14T04:54:12.373 回答