SQL查询-如果两者都没有,则显示



我有一个表,看起来像:

表名:";交互作用";

id: number 
type: text
user_id: number

我想得到所有具有类型为"的行的user_id;类型A";但不具有类型为"="的行;类型B";

这可能吗?

您可以尝试使用not exists

select distinct user_id from interactions i
where type='type_A' and 
not exists (select 1 from interactions i1 where i.user_id=i1.user_id and type='type_B')

另一种选择可能是

select user_id from interactions
group by user_id 
having min(type)='type_A' and max(type)='type_A'

您可以在SQL 中使用AND条件

它是这样的:

WHERE条件1AND条件2。。。AND condition_n;

请参阅文档https://www.techonthenet.com/mysql/and.php

此外,你可以让它看起来不等于某个东西。

相关内容

最新更新