我正在嘗試使用 Set 運算子來顯示沒有城市(Table B col.)的國家名稱(Table A col.)和沒有國家(A)的城市(B)。我還嘗試使用 LEFT JOIN 撰寫此查詢,如下所示,我包括表 C(Regions),因為我不確定是否在 LEFT JOIN 中使用該主鍵。
表 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 值,以及不包含表 B 值的表 A 值(沒有 Cities 的國家/地區)。
我也在下面嘗試了這個陳述句并得到了完全相同的結果:
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 中找不到表 A 值(在城市中找不到國家。)
uj5u.com熱心網友回復:
有很多選擇可以得到你想要的結果。一種方法是使用LEFT JOIN得到沒有城市的國家和RIGHT 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;
另一種可能性是使用兩個LEFT 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,可以使用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);
或者,如果您愿意NOT EXISTS,這也可以:
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<>fiddle
如果您希望結果集按它們排序,請將ORDER BY c.country_name和添加到查詢中。ORDER BY l.city
最后但重要的注意事項:如您所見,我使用UNION ALL而不是UNION因為我看不出UNION在您的用例中使用的原因。UNION ALL速度要快得多,所以我建議使用它,除非有真正令人信服的理由不使用它。唯一的優點UNION是它不會顯示重復的行,但我認為在您的情況下它們不太可能出現,因此不需要它。
uj5u.com熱心網友回復:
使用集合運算子查找沒有城市的國家的最簡單示例是:
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
沒有國家(或國家代碼無效)的城市作為左連接和過濾器更簡單:
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 為空的位置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/482539.html
