我有以下查询,它为我提供了准确的结果:
SELECT t.id
FROM titles t
ORDER BY t.id
Run Code Online (Sandbox Code Playgroud)
我的结果是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Run Code Online (Sandbox Code Playgroud)
我的第二个查询也为我提供了准确的结果:
SELECT t.id
FROM titles t
JOIN subscriptions s
ON t.id = s.title
WHERE s.user=2
Run Code Online (Sandbox Code Playgroud)
结果:
10
11
14
Run Code Online (Sandbox Code Playgroud)
所以我想要做的是从第一个查询中收到第二个查询中没有显示的所有结果,所以我运行这个:
SELECT t.id
FROM titles t
ORDER BY t.id NOT IN
(
SELECT t.id
FROM titles t
JOIN subscriptions s
ON t.id = s.title
WHERE s.user=2
);
Run Code Online (Sandbox Code Playgroud)
但我的结果最终结果如下:
14
11
10
13
12
9
8
7
6
5
4
3
2
1
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?为什么我的第二个查询中的订单被颠倒了?
NOT IN应该是WHERE条件的一部分,而不是ORDER BY声明:
SELECT
t.id
FROM
titles t
WHERE
t.id NOT IN
(
SELECT t.id
FROM titles t
JOIN subscriptions s
ON t.id = s.title
WHERE s.user=2
)
ORDER BY
t.id
Run Code Online (Sandbox Code Playgroud)