简体   繁体   English

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

[英]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.

相关问题 SQL 查询从一个表中获取不同的结果并将这些结果与表中的另一列匹配 - SQL Query to get distinct results from one table and match those results to the another column in the table SQL查询,其中来自一个表的数据=来自另一个表的数据 - SQL query where data from one table = data from another 从另一个表中 ID 不匹配的列更新一个表中列的值 - Update value of column in one table from a column in another table where ID's do not match Oracle - SQL 查询根据列值将数据从一个表分装到另一个表? - Oracle - SQL query to bin the data from one table to another based on a column value? 查询一个表,其中该表中的列包含另一个表上的列中的值 - Query one table where a column in that table contains a value from a column on another table SQL将一列插入另一个匹配的表 - SQL Insert one column into another table where there is a match SQL查询从一个表中获取数据,而从另一表中仅获取一行数据 - SQL Query get data from one table, and get data from only one row from another table Oracle-SQL查询以从另一个表中的列返回值,其中值等于另一个列 - Oracle - SQL Query to return a value from a column in another table where value equals a different column 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? SQL将一个表中的列值与另一个表中的列值匹配,而无需联接 - SQL match column value from one table with column values in another without joining
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM