SQL Plus 如果同一帐户没有两个不同的结果,如何只显示行
SQL Plus How to only display rows if it does not have two different results for the same account
我希望有人可以帮助仅显示仅由 user_name = 'system'
修改的帐户
示例table_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
我基本上是在尝试创建一个查询,如果有 'system' 以外的任何内容,则不显示该 ID 的任何记录。因此,对于上面输出中的示例,我不希望显示 ID 26 的任何行(即使是由系统更新的行),因为其中一条记录是由 'system' 以外的其他内容更新的。我只想显示 ID 14,因为所有记录都由 user_name 'system'
更新
最终输出应该是这样的
ID
14
如果需要详细信息,请使用 not exists
或 not 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
值的方式略有不同。
我希望有人可以帮助仅显示仅由 user_name = 'system'
修改的帐户示例table_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
我基本上是在尝试创建一个查询,如果有 'system' 以外的任何内容,则不显示该 ID 的任何记录。因此,对于上面输出中的示例,我不希望显示 ID 26 的任何行(即使是由系统更新的行),因为其中一条记录是由 'system' 以外的其他内容更新的。我只想显示 ID 14,因为所有记录都由 user_name 'system'
更新最终输出应该是这样的 ID 14
如果需要详细信息,请使用 not exists
或 not 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
值的方式略有不同。