简体   繁体   English

过滤表中的数据

[英]Filtering data in tables

I have two tables account_agent and bulk_index .我有两个表account_agentbulk_index The data on each table is as follows每个表上的数据如下

account_agent

id         name         
1          Tom           
2          Brad           
3          John           
4          Jan        
5          Bartosz 

account_agent to filter

id         name         
1          Tom              
3          John              
5          Bartosz 

using these tables I want to make a normalized view that contains all agents from first table, but without agents placed in second table.使用这些表我想制作一个标准化视图,其中包含第一个表中的所有代理,但没有放置在第二个表中的代理。

account_agent_filtered

name         code         
2            Brad 
4            Jan  

I have a lot of data and with cross join I could just do an opposite thing, but now I do not want to match.我有很多数据,通过交叉连接我可以做相反的事情,但现在我不想匹配。 I want to filter and I do not have any idea how to do this.我想过滤,但我不知道该怎么做。

Unfortunately, MySQL doesn't have an except set operator, but you could emulate this behavior using exists :不幸的是,MySQL 没有except set 运算符,但您可以使用exists模拟这种行为:

SELECT *
FROM   account_agent aa
WHERE  NOT EXISTS (SELECT *
                   FROM   account_agent_to_filter aatf
                   WHERE  aa.id = aatf.id AND aa.name = aatf.name)

In addition to Mureinik 's answer :除了Mureinik回答

SELECT t1.*
FROM account_agent t1
LEFT JOIN account_agent_to_filter t2 USING (id, name)
WHERE t2.id IS NULL

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

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