SELECT CountryCode, Name AS Country, Language
FROM Country INNER JOIN CountryLanguage
ON Country.Code = CountryLanguage.CountryCode
WHERE Language = 'German'
ORDER BY Country;
如何使用此SELECT语句使用EXISTS和子查询?
答案 0 :(得分:0)
这应该产生相同的结果并使用EXISTS:
select countrycode, name as country, language
from country y
where exists (select 'fish'
from countrylanguage x
where x.countrycode = y.code
and x.language = 'German')
order by country;
使用EXISTS子查询,如果子查询返回任何内容,则条件通过,如果它没有返回任何行,则它会失败,无论你在select语句中放入什么(注意我输入'fish')。但是,在该国家的语言为德语的国家语言中,该子查询仍将返回一行,因此对于适用于您的国家行,条件通过,因此它以相同的方式过滤。
答案 1 :(得分:0)
感谢您的所有提示。我能够用EXISTS和子查询修补一个解决方案,产生与原始SELECT语句相同的结果。
SELECT CountryCode, Name AS Country, Language
FROM CountryLanguage AS X INNER JOIN Country AS Y
ON X.CountryCode = Y.Code
WHERE EXISTS
(
SELECT 'German'
FROM CountryLanguage AS X
WHERE X.CountryCode = Y.Code
AND X.Language = 'German'
)
AND X.Language = 'German'
ORDER BY Country;