【MySQL】行轉列

給定學生表以下mysql

create table student
id int primary key,
name varchar(11),
course varchar(11),
score int

請用一條sql查出 >80分的科目列表,且用一行記錄展現sql

SELECT 
  name AS studentName,
  JSON_ARRAYAGG(course) AS courseNameList
FROM student
WHERE score > 80
GROUP BY studentName;

參考

https://database.guide/json_arrayagg-create-a-json-array-from-the-rows-of-a-query-in-mysql/json

相關文章
相關標籤/搜索