我想我误解了 SQL。当我尝试以下矛盾的查询时:
SELECT id, key, value FROM Things WHERE value > 1 AND key = 'key' AND id IN (
SELECT id
FROM Things
WHERE key = 'key' AND value < 1
) GROUP BY id, key, value LIMIT 2;
我得到以下结果:
id | key | value
--------+------------+-----------
92 | 18 | 2
4 | 18 | 2
同样,当我尝试此查询时:
SELECT id, key, value FROM Things a WHERE value > 1 AND key = 'key' AND EXISTS (
SELECT *
FROM Things b
WHERE a.id = b.id AND a.key = b.key AND b.value < 1
) GROUP BY id, key, value LIMIT 2;
我得到相同的结果:
id | key | value
--------+------------+-----------
92 | 18 | 2
4 | 18 | 2
最后,当我尝试这个时,我得到了正确的结果:
SELECT X.id, X.key, X.value FROM (
SELECT id, key, value
FROM Things
WHERE key = 'key' and value < 1
) AS X WHERE X.key = 'key' AND X.value > 1 GROUP BY id, key, value LIMIT 2;
id | key | value
--------+------------+-----------
我想也许索引在弄乱我,但我确保没有索引并且它仍然表现得像这样。
有人可以解释为什么会发生这种情况以及我误解了什么吗?