Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 按国家分组,带桥接表_Sql_Database - Fatal编程技术网

Sql 按国家分组,带桥接表

Sql 按国家分组,带桥接表,sql,database,Sql,Database,我不太确定下表是否可以编写一个返回有社区的国家的查询。CommunityLocationbridge表是我可以查找公司及其位置的地方,但我不太确定如何真正塑造这个查询 Community community_id Location location_id country_id Country country_id CommunityLocation community_id location_id 所以我只需要一个有社区的国家ID列表。我想这就是您需要的: sel

我不太确定下表是否可以编写一个返回有社区的国家的查询。
CommunityLocation
bridge表是我可以查找公司及其位置的地方,但我不太确定如何真正塑造这个查询

Community
  community_id

Location
  location_id
  country_id

Country
  country_id

CommunityLocation
  community_id
  location_id

所以我只需要一个有社区的国家ID列表。

我想这就是您需要的:

select distinct
   c.country_id 
from 
   countires c
join Location l
   on c.country_id = l.country_id
join CommunityLocation cl
   on cl.location_id = l.location_id

我想这就是你需要的:

select distinct
   c.country_id 
from 
   countires c
join Location l
   on c.country_id = l.country_id
join CommunityLocation cl
   on cl.location_id = l.location_id

我建议
存在

select c.country_id 
from countries c
where exists (select 1
              from Location l join
                   CommunityLocation cl
                   on cl.location_id = l.location_id
              where c.country_id = l.country_id
             );

这避免了为删除重复项而进行的额外处理,这通常是一种性能提升。

我建议
exists

select c.country_id 
from countries c
where exists (select 1
              from Location l join
                   CommunityLocation cl
                   on cl.location_id = l.location_id
              where c.country_id = l.country_id
             );

这避免了删除重复项的额外处理,这通常会提高性能。

close我想这里也需要一个群组。我添加了c.country\u id的
组,它可以正常工作。非常感谢。是不是按你需要的分组,没有区别?@PositiveGuy这很重要,他们会做同样的事情(在这个解决方案中)我想我也需要一个分组。我添加了c.country\u id的
组,它可以正常工作。非常感谢。不是按你需要的分组,而不是区分吗?@PositiveGuy这有关系吗,他们会做同样的事情(在这个解决方案中)