gpt4 book ai didi

mysql - 如何写这个查询? (无现有项目)

转载 作者:行者123 更新时间:2023-11-29 00:29:04 24 4
gpt4 key购买 nike

这是我的国家表:

id | name

这是我的语言表:

id | name

这是连接它们的 countryLanguages 表:

id | country_id | language_id

现在我想写一个查询来做出这样的选择

language_id | language_name

国家/地区 id=3 的 countryLanguages 表中不存在语言。

例子:

countries:
1 | US
2 | UK
3 | CHINA

languages:
1 | chinese
2 | spanish
3 | english

countryLanguages:
1 | 1 | 3
2 | 1 | 2
3 | 2 | 3
4 | 3 | 1

应该返回:

2 | spanish
3 | english

因为在中国不说西类牙语和英语。

最佳答案

您可以使用如下子查询实现此目的:

# this is the outer select for every language not in the list returned from the inner query
SELECT * FROM languages WHERE languages.id NOT IN (
# this is the inner select for every language spoken in country 3
SELECT
languages.id
FROM
countries, countryLanguages, languages
WHERE
countries.id = countryLanguages.country_id AND
languages.id = countryLanguages.language_id AND
countries.id = 3
);

请注意,子查询并不总是做某事的最快方法,您通常可以想出一个切肉刀连接,但这应该可以满足您的需求。

关于mysql - 如何写这个查询? (无现有项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17457206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com