gpt4 book ai didi

Mysql查询组通过有

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:51 24 4
gpt4 key购买 nike

我有一个包含 3 列的表格:id、句子和语言。所以句子可以是英语和德语,ID 被分配给具有相同含义但不同语言的句子,比如

ID | sentence | language
1 | Hello | en
1 | Hallo | de
2 | Sorry | en

可能存在只存在于一种语言中的句子。现在我想找出所有可用两种语言的句子,我可以这样做:

SELECT 
*
FROM
`sentences`
WHERE
LENGTH(sentence) > 0
AND (language = 'en' OR language = 'de')
GROUP BY id
HAVING COUNT(language) = 2

我只得到德语句子的结果。那我做

SELECT 
*
FROM
sentences
WHERE
id IN (SELECT
id
FROM
`sentences`
WHERE
LENGTH(sentence) > 0
AND (language = 'en' OR language = 'de')
GROUP BY id
HAVING COUNT(language) = 2)

这应该可行,但查询需要很长时间。我的问题:有没有什么奇特的方法可以做到这一点?

最佳答案

INNER JOINS 比使用 IN 子句更快

SELECT en.id, 
en.sentence as en_sentence,
de.sentence as de_sentence,
en.language as en_language,
de.language as de_language
FROM sentences en
INNER JOIN sentences de ON en.ID = de.ID AND en.language = 'en' AND de.language = 'de'
WHERE length(en.sentence) > 0
AND length(de.sentence) > 0

关于Mysql查询组通过有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37532673/

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