如何优化NOT IN查询

时间:2014-12-11 16:42:08

标签: mysql

SELECT * 
FROM '.PRFX.'sell 
WHERE draft = "0" '.$e_sql.' 
AND ID NOT IN (SELECT id_ FROM '.PRFX.'skipped WHERE uid = "'.$u.'") 
AND ID NOT IN (SELECT id_ FROM '.PRFX.'followed WHERE uid = "'.$u.'") 
ORDER BY raised DESC '.$sql_limit

使用例如加入(会有帮助吗?)

1 个答案:

答案 0 :(得分:-1)

我只是在学校学习SQL,所以我会尽力提交一个不错的答案。

如果你想要你的子查询表'加入你可能会使用这个:

SELECT * 
FROM '.PRFX.'sell 
WHERE draft = "0" 
'.$e_sql.' 
AND ID NOT IN 
    (SELECT id_ 
    FROM '.PRFX.'skipped 
    WHERE uid = "'.$u.'") AS idSkipped
    INNER JOIN
    (SELECT id_ 
    FROM '.PRFX.'followed 
    WHERE uid = "'.$u.'") AS idFollowed
    ON idSkipped.id_ = idFollowed.id_
ORDER BY raised DESC '.$sql_limit'

您可能不想使用INNER JOIN,因为如果在其他表格中找不到,则会丢弃任何id_。

我希望这有帮助!
伯特