我有这句话:
SELECT id, name FROM players WHERE this = 1 AND name NOT IN (SELECT name FROM players_online WHERE this = 'that') ORDER BY RAND() LIMIT 3, 6
所有列均已正确命名。没有SQL错误发生,它只是不返回任何结果。
根据我在文档中阅读的内容,它应该可以,但不能。
任何的想法?
不要NOT IN与子查询一起使用。如果子查询中的任何值为,则语义错误NULL。在这种情况下,NOT INnever的取值为TRUE,因此根本不返回任何行。
NOT IN
NULL
而是使用NOT EXISTS:
NOT EXISTS
SELECT p.id, p.name FROM players p WHERE p.this = 1 AND NOT EXISTS (SELECT 1 FROM players_online po WHERE po.name = p.name AND po.this = 'that' ) ORDER BY RAND() LIMIT 3, 6;