繁体   English   中英

sql查询从审计表中获取数据,其中列匹配一个值,但不匹配另一个

[英]sql query to get data from an audit table where column match one value, but not another

我有一个包含 4 列的表t_audit :名称、地址、更新(是或t_audit )和 user_id(更新它的用户的 ID)。

我想搜索审计表,以获取由 user_id 1 而不是 2 更新的客户名称。

我试过这个:

select ta.name 
from t_audit ta 
where (ta.updated = 'Y' and user_id = 1 ) 
    and not exists (ta.updated='Y' and user_id = 2)

但它不起作用。 我怎样才能解决这个问题?

甲骨文

select ta.name 
from t_audit ta 
where (ta.updated = 'Y' and user_id = 1 ) 
minus
select ta.name 
from t_audit ta 
where (ta.updated = 'Y' and user_id = 2 ) 

为他人

select ta.name 
from t_audit ta 
where (ta.updated = 'Y' and user_id = 1 ) 
EXCEPT
select ta.name 
from t_audit ta 
where (ta.updated = 'Y' and user_id = 2 ) 

不存在的语法不正确,以下内容未经测试当然应该接近您的期望

select ta.name 
from t_audit ta 
where ta.updated = 'Y' and ta.user_id = 1  
  and not exists (
    select * from t_audit ta2
    where ta2.name=ta.name and ta2.updated='Y' and ta2.user_id = 2
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM