SQL:仅返回没有任何符合条件的关联的记录 [英] SQL: Return only records without any associations that meet criteria

查看:131
本文介绍了SQL:仅返回没有任何符合条件的关联的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅返回没有任何符合特定条件的关联记录的记录?

How can I return only the records that do not have any associated records that meet a certain criteria?

例如,如果我与用户和地址之间存在一对多的关系,我如何获得所有在地址历史记录中没有特定城市的用户?

For example, if I have a one to many relationship between users and addresses, how can I get all the users who do NOT have a specific city in their address history?

SQL小提琴此处

示例数据在这里:

CREATE TABLE Users
  (`id` int, `name` varchar(20))
;

CREATE TABLE Addresses
  (`id` int, `city` varchar(20),`user_name` varchar(20))
;

INSERT INTO Users
  (`id`, `name`)

VALUES
  (1, 'sarah'),
  (2, 'harry'),
;

INSERT INTO Addresses
  (`id`, `city`, `user_name`)

VALUES
  (1, 'denver', 'sarah'),
  (2, 'anchorage', 'sarah'),
  (3, 'providence', 'harry'),
  (4, 'new york', 'harry')
;

推荐答案

最简单的方法是使用not existsleft join:

The simplest way is with not exists or left join:

select u.*
from users u left join
     addresses a
     on a.username = u.username and
        a.city = 'Peoria'
where a.city is null;

left join保留用户中的所有记录以及addresses中符合on条件的所有记录.在这种情况下(因为城市名称处于on条件),它将返回所有具有城市信息或NULL值的用户. where子句选择NULL值-不匹配的值.

The left join keeps all records in users and any records in addresses that match the on conditions. In this case (because the city name is in the on condition), it returns all users with either information about the cities or NULL values. The where clause chooses the NULL values -- the non-matching ones.

等效的not exists可能更容易理解:

The equivalent not exists might be easier to follow:

select u.*
from users u 
where not exists (select 1
                  from addresses a
                  where a.username = u.username and
                        a.city = 'Peoria'
                 );

这篇关于SQL:仅返回没有任何符合条件的关联的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆