gpt4 book ai didi

mysql - SQL Plus 如果同一帐户没有两个不同的结果,如何只显示行

转载 作者:行者123 更新时间:2023-11-28 23:32:06 24 4
gpt4 key购买 nike

我希望有人可以帮助仅显示仅由 user_name = 'system' 修改的帐户

示例表_1

ID
USER_NAME
DATE_TIME
UPDATES

假设我愿意

select * from table_1 where ID='14'
ID USER_NAME UPDATES
14 system deleted record
14 system updated record
14 system updated record
14 system copied record

假设我愿意

select * from table_1 where ID='26'
ID USER_NAME UPDATES
26 system updated record
26 system deleted record
26 Michael updated record
26 system updated record

我基本上是在尝试创建一个查询,如果有除“系统”以外的任何内容,则不显示该 ID 的任何记录。因此,对于上面输出中的示例,我不希望显示 ID 26 的任何行(即使是由系统更新的行),因为其中一条记录是由“系统”以外的其他内容更新的。我只想显示 ID 14,因为所有记录都由用户名“系统”更新

最终输出应该是这样的ID14

最佳答案

使用 not existsnot in如果您需要详细信息:

select t.*
from table_1 t
where not exists (select 1
from table_1 tt
where tt.id = t.id and tt.user_name <> 'system'
);

如果您只想要 id , 然后使用 group by :

select t.id
from table_1 t
group by t.id
having sum(t.user_name = 'system') = count(*); -- all rows have `system`

注意:您也可以使用 having sum(t.user_name <> 'system') = 0 .然而,这两个句柄NULL值(value)观略有不同。

关于mysql - SQL Plus 如果同一帐户没有两个不同的结果,如何只显示行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37039093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com