如何查询列中不可用的值?

How to query not available value in the column?

我有一个table如下:

据此,我正在尝试获取 user_id 的状态 IS NOT STATUS 4 AVAILABLE。换句话说,我期望的结果是:

User Ids: 17, 19 and 20.

我尝试了很多查询,但没有一个对我有用。比如我试过了。

SELECT user_id WHERE status NOT IN (4)

上面的查询还是returns如下:

User Ids: 1, 17, 18, 19 and 20.

我知道它返回的原因,但我不确定如何解决这个问题。

如有任何帮助,我们将不胜感激。

您将需要为此执行子查询。例如:

 Select user_id from table Where user_id not in 
         (select user_id from table where status = 4 and user_id is not null);

这允许您排除状态为 4 的所有 user_id。

试试这个:

SELECT user_id
FROM mytable
GROUP BY user_id
HAVING COUNT(CASE WHEN status = 4 THEN 1 END) = 0

或者,或者:

SELECT user_id
FROM mytable AS t1
WHERE NOT EXISTS (SELECT 1
                  FROM mytable AS t2
                  WHERE t1.user_id = t2.user_id AND status = 4)