SQL IN和AND子句输出

use*_*579 2 sql

我写了一个像下面这样的小查询.它给了我输出.

select user_id 
from table tf
where tf.function_id in ('1001051','1001060','1001061')
Run Code Online (Sandbox Code Playgroud)

但是,当我运行如下所示的查询时,它显示0输出.但是我已经手动验证了我们有user_id,其中所有3个function_id都存在.

select user_id 
from table tf
where tf.function_id='1001051' 
      and 
      tf.function_id='1001060' 
      and 
      tf.function_id='1001061'
Run Code Online (Sandbox Code Playgroud)

使用AND子句看起来很简单.但是我没有得到理想的输出.难道我做错了什么?

提前致谢

Gor*_*off 7

这是你想要做的吗?

select tf.user_id
from table tf
where tf.function_id in ('1001051', '1001060', '1001061')
group by tf.user_id
having count(distinct tf.function_id) = 3;
Run Code Online (Sandbox Code Playgroud)

这将返回具有所有三个功能的用户.

编辑:

这是您评论中的查询:

select tu.dealer_id, tu.usr_alias, tf.function_nm
from t_usr tu, t_usr_function tuf, t_function tf
where tu.usr_id = tuf.usr_id and tuf.function_id = tf.function_id and
      tf.function_id = '1001051' and tf.function_id = '1001060' and tf.function_id = '1001061' ;
Run Code Online (Sandbox Code Playgroud)

首先,您应该学习正确的join语法.简单规则:不要在from子句中使用逗号.

我认为你想要的查询是:

select tu.dealer_id, tu.usr_alias
from t_usr tu join
     t_usr_function tuf
     on tu.usr_id = tuf.usr_id 
where tuf.function_id in ('1001051', '1001060', '1001061')
group by tu.dealer_id, tu.usr_alias
having count(distinct tuf.function_id) = 3;
Run Code Online (Sandbox Code Playgroud)

这不会给你功能名称.如果每个"用户"(或至少是经销商/用户别名组合)都有三个功能,我不确定为什么需要这样的细节.并且,原始问题不要求这种详细程度.