gpt4 book ai didi

mysql - 如何使用 GROUP_CONCAT 在 GROUP BY 之后加入?

转载 作者:行者123 更新时间:2023-11-29 05:47:40 25 4
gpt4 key购买 nike

我有两个表

CREATE TABLE texts
(
TextID int(11) unsigned,
SentenceID int(11) unsigned,
Phrase tinyint(3) unsigned,
Word varchar(255),
PRIMARY KEY(TextID,SentenceID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci

CREATE TABLE phrases
(
PhraseID int(11) unsigned NOT NULL AUTO_INCREMENT,
Phrase varchar(255),
PRIMARY KEY(PhraseID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci

我通过 GROUP BY 从文本表中获取短语,

SELECT TRIM(GROUP_CONCAT(Word SEPARATOR ' '))  AS GroupedPhrase
FROM texts
WHERE Phrase IS NOT NULL GROUP BY TextID,SentenceID,Phrase

但我需要JOIN 两个表来获取PhraseID 而不是Phrase,执行此操作的查询:

SELECT a.TextID,a.SentenceID,b.PhraseID 
FROM texts a JOIN phrases b ON TRIM(GROUP_CONCAT(a.Word SEPARATOR ' '))=b.Phrase
WHERE a.Phrase IS NOT NULL GROUP BY a.TextID,a.SentenceID,a.Phrase

最佳答案

使用子查询:

SELECT *
FROM (SELECT TextID,SentenceID,TRIM(GROUP_CONCAT(Word SEPARATOR ' ')) AS GroupedPhrase
FROM texts
WHERE Phrase IS NOT NULL
GROUP BY TextID,SentenceID,Phrase) sub
JOIN phrases b ON sub.GroupedPhrase=b.Phrase

关于mysql - 如何使用 GROUP_CONCAT 在 GROUP BY 之后加入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249814/

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