gpt4 book ai didi

MySQL Select 查询以根据列表值获取记录

转载 作者:行者123 更新时间:2023-11-29 01:27:35 25 4
gpt4 key购买 nike

我正在使用 MySQL。我有 3 个表,如下所示。

表:subject_Master

--------------------------
subjectId | subjectShortName
----------------------------------
1 | English
2 | French
3 | German
4 | Latin
----------------------------------

表:class_Master

-----------------------------------
classId | className
----------------------------------
1 | Rose
2 | Dasy
3 | Lily

表:主题分配

------------------------------------------
allocationId | classId | subjectId
-------------------------------------------
1 | 1 | 1,2
2 | 2 | 2,3,4
3 | 3 | 1,2,3,4

我如何获得如下 SQL 结果,想要为 subjectAllocation 行中的每个 subjectId 获取 SubjectName

ClassName   |   SubjectName
-------------------------------------------
Rose | English,French
Dasy | French,German,Latin
Lily | English,French,German,Latin

最佳答案

使用FIND_IN_SET()功能:

试试这个:

SELECT A.allocationId, 
B.className,
GROUP_CONCAT(C.subjectShortName) AS subjectName
FROM subjectAllocation A
INNER JOIN class_Master B ON A.classId = B.classId
INNER JOIN subject_Master C ON FIND_IN_SET(C.subjectId, A.subjectId)
GROUP BY A.allocationId;

关于MySQL Select 查询以根据列表值获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553775/

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