[英]sql query to get data from an audit table where column match one value, but not another
I have a table t_audit
with 4 columns: name, address, updated (Y or N), and user_id (ID of the user who updates it).我有一个包含 4 列的表
t_audit
:名称、地址、更新(是或t_audit
)和 user_id(更新它的用户的 ID)。
I want to search the audit table, to get the names of customers updated by user_id 1, but not 2.我想搜索审计表,以获取由 user_id 1 而不是 2 更新的客户名称。
I tried this:我试过这个:
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)
But it does not work.但它不起作用。 How can I fix this?
我怎样才能解决这个问题?
for Oracle甲骨文
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 )
for others为他人
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 )
Your not-exists syntax is incorrect, the following is untested of course by should be close to what you are expecting您不存在的语法不正确,以下内容未经测试当然应该接近您的期望
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.