查询使用设置运算符显示特定列

发布于 2025-02-02 22:16:46 字数 1072 浏览 4 评论 0原文

我试图使用设定的操作员在没有城市的情况下显示国家名称(表A col。)和没有国家(a)的城市(b)(b)。我还尝试使用左连接来编写此查询,我在下面显示,并且包括表C(区域),因为我不确定是否在左联接中使用该主键。

表A(国家):

Column_name   |   Column_id
              |
COUNTRY_ID    |     1
COUNTRY_NAME  |     2
REGION_ID     |     3

表B(位置):

Column_name   |   Column_id
              |
LOCATION_ID   |     1
   CITY       |     4
COUNTRY_ID    |     6

表C(区域):

Column_name   |   Column_id
              |
 REGION_ID    |     1

我尝试了以下内容:

SELECT c.country_name, l.city
FROM Countries c
LEFT JOIN Locations l ON c.country_id = l.country_id

UNION

SELECT c2.country_name, l2.city
FROM Countries c2
LEFT JOIN Locations l2 ON c2.country_id = l2.country_id;

上面的SQL语句返回了所有表A值,表A值不包含表B值(不包含表B值的国家)有城市)。

我还尝试了下面的此说明,并得到了完全相同的结果:

SELECT c.country_name, l.city
FROM Countries c
LEFT JOIN Locations l ON c.country_id = l.country_id
LEFT JOIN Regions r ON r.region_id = c.region_id;

缺少的一件事是表B中未在表B中找到的值(城市中未找到的国家)。

I am trying to use a Set operator to show country names(Table A col.) without a city(Table B col.) and cities(B) without a country(A). I have also tried to write this query using LEFT JOINS, which I show below and I included Table C(Regions) because I am not sure whether to use that primary key in a LEFT JOIN.

Table A (Countries):

Column_name   |   Column_id
              |
COUNTRY_ID    |     1
COUNTRY_NAME  |     2
REGION_ID     |     3

Table B (Locations):

Column_name   |   Column_id
              |
LOCATION_ID   |     1
   CITY       |     4
COUNTRY_ID    |     6

Table C(Regions):

Column_name   |   Column_id
              |
 REGION_ID    |     1

I have tried the following:

SELECT c.country_name, l.city
FROM Countries c
LEFT JOIN Locations l ON c.country_id = l.country_id

UNION

SELECT c2.country_name, l2.city
FROM Countries c2
LEFT JOIN Locations l2 ON c2.country_id = l2.country_id;

The SQL statement above returned all Table A values, and Table A values that do not contain Table B values (Countries that do not have Cities).

I also tried this statement below and got the exact same result:

SELECT c.country_name, l.city
FROM Countries c
LEFT JOIN Locations l ON c.country_id = l.country_id
LEFT JOIN Regions r ON r.region_id = c.region_id;

The one thing it is missing is Table A values not found in Table B (Countries not found in Cities.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2025-02-09 22:16:46

有很多选择可以取得理想的结果。一种方法是使用左JOIN以获取没有城市的国家,右JOIN以无国界获取城市:

SELECT c.country_name, l.city
FROM countries c
LEFT JOIN locations l ON c.country_id = l.country_id
WHERE l.city IS NULL
UNION ALL
SELECT c.country_name, l.city
FROM countries c
RIGHT JOIN locations l ON c.country_id = l.country_id
WHERE c.country_name IS NULL;

另一种可能性是使用两个左JOIN加入,但是从相反的表开始,这样:

SELECT c.country_name, l.city
FROM countries c
LEFT JOIN locations l ON c.country_id = l.country_id
WHERE l.city IS NULL
UNION ALL
SELECT c.country_name, l.city
FROM locations l
LEFT JOIN countries c ON c.country_id = l.country_id
WHERE c.country_name IS NULL;

如果您不喜欢使用JOIN,则可以使用中的进行此操作:

SELECT c.country_name, NULL city
FROM countries c
WHERE country_id NOT IN (SELECT country_id FROM locations)
UNION ALL
SELECT NULL country_name, l.city
FROM locations l
WHERE country_id NOT IN (SELECT country_id FROM countries);

或者,如果您喜欢不存在,这也将起作用:

SELECT c.country_name, NULL city
FROM countries c
WHERE NOT EXISTS (SELECT 1 FROM locations WHERE country_id = c.country_id)
UNION ALL
SELECT NULL country_name, l.city
FROM locations l
WHERE NOT EXISTS (SELECT 1 FROM countries WHERE country_id = l.country_id);

我创建了一个示例,显示所有这些查询将产生身份结果: db&lt

;您希望将结果设置为它们。

最后一个但重要的说明:如您所见,我使用了Union all而不是Union,因为我看不出为什么使用Union 在您的用例中。 联合所有的速度要快得多,因此我建议使用它,除非有真正令人信服的理由不使用它。 Union的唯一优点是它没有显示重复的行,但我认为它们在您的情况下不太可能,因此不需要。

There are a lot of options to get your desired result. One way is to use LEFT JOIN to get the countries without city and RIGHT JOIN to get the cities without country:

SELECT c.country_name, l.city
FROM countries c
LEFT JOIN locations l ON c.country_id = l.country_id
WHERE l.city IS NULL
UNION ALL
SELECT c.country_name, l.city
FROM countries c
RIGHT JOIN locations l ON c.country_id = l.country_id
WHERE c.country_name IS NULL;

Another possibility is to use two LEFT JOIN, but starting from the opposite table, like this:

SELECT c.country_name, l.city
FROM countries c
LEFT JOIN locations l ON c.country_id = l.country_id
WHERE l.city IS NULL
UNION ALL
SELECT c.country_name, l.city
FROM locations l
LEFT JOIN countries c ON c.country_id = l.country_id
WHERE c.country_name IS NULL;

If you don't like using JOIN at all, you can do this using NOT IN:

SELECT c.country_name, NULL city
FROM countries c
WHERE country_id NOT IN (SELECT country_id FROM locations)
UNION ALL
SELECT NULL country_name, l.city
FROM locations l
WHERE country_id NOT IN (SELECT country_id FROM countries);

Or if you prefer NOT EXISTS, this will work, too:

SELECT c.country_name, NULL city
FROM countries c
WHERE NOT EXISTS (SELECT 1 FROM locations WHERE country_id = c.country_id)
UNION ALL
SELECT NULL country_name, l.city
FROM locations l
WHERE NOT EXISTS (SELECT 1 FROM countries WHERE country_id = l.country_id);

I created an example that shows all these queries will produce the identic outcome: db<>fiddle

Add ORDER BY c.country_name and ORDER BY l.city to the queries in case you want the result set to be sorted by them.

A last, but important note: As you see, I used UNION ALL instead of UNION because I don't see a reason why to use UNION in your use case. UNION ALL is much faster, so I recommend to use that unless there is a really convincing reason to do not use it. The only advantage of UNION is that it does not show duplicate rows, but I think they are very unlikely in your situation, so it should not be required.

沉睡月亮 2025-02-09 22:16:46

最简单的说明是使用集合操作员在没有城市的情况下找到国家 /地区的最简单说明是:

select country_id from countries
minus
select country_id from locations
COUNTRY_ID
----------
         1
         4

由于您需要的国家名称,您只需要查找它:

select country_name from countries
minus
select c.country_name from locations l
       join countries c on c.country_id = l.country_id;
COUNTRY_NAME
-----------------
England
Italy

没有国家(或使用无效的国家代码)的城市,因为左JOIN和过滤器:

select l.city, l.country_id
from   locations l
       left join countries c on c.country_id = l.country_id
where  c.country_id is null
CITY             
-----------------
Berlin           
Tokyo            

如果确实需要使用集合操作员来执行此操作,那么您会(从概念上)寻找country_id在(位置国家 /地区国家国家 /地区)中的城市:

select l.location_id, l.city, l.country_id
from   locations l
where  l.country_id in
       ( select country_id from locations
         minus
         select country_id from countries )

但是,这不会给您带来country_id的位置。

The simplest illustration of using a set operator to find countries without cities would be:

select country_id from countries
minus
select country_id from locations
COUNTRY_ID
----------
         1
         4

As you need the country name, you just need to look it up:

select country_name from countries
minus
select c.country_name from locations l
       join countries c on c.country_id = l.country_id;
COUNTRY_NAME
-----------------
England
Italy

Cities without a country (or with an invalid country code) is simpler as a left join and filter:

select l.city, l.country_id
from   locations l
       left join countries c on c.country_id = l.country_id
where  c.country_id is null
CITY             
-----------------
Berlin           
Tokyo            

If the requirement really is to do this using set operators, you would (conceptually) look for cities whose country_id is in the set of (location countries minus city countries):

select l.location_id, l.city, l.country_id
from   locations l
where  l.country_id in
       ( select country_id from locations
         minus
         select country_id from countries )

However this wouldn't give you locations whose country_id was null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文