将行值添加到单列中 MYSQL
add row values into single column MYSQL
我有这个数据。
create table student
(
student_id int,
name varchar(50)
)
create table student_option
(
student_option_id int,
student_id int,
s_option varchar(50)
)
insert into student
(
student_id,
name
)values(1,'John'),(2,'Martin')
insert into student_option
(
student_option_id,
student_id,
s_option
)values(1,1,'A'),(2,1,'B'),(3,2,'C'),(4,2,'D')
select * from student
select * from student_option
我希望输出显示为
student_name options
John A, B
Martin C, D
在 MYSQL5.6
中执行此操作的最优化方法是什么
提前致谢
这个应该可以。你基本上使用 GROUP_CONCAT function from MySQL 和 GROUP BY name after.
select s.name, group_concat(o.s_option)
from student s
left join student_option o on (s.student_id = o.student_id)
group by s.name;
结果:
name s_option
John B,A
Martin D,C
我有这个数据。
create table student
(
student_id int,
name varchar(50)
)
create table student_option
(
student_option_id int,
student_id int,
s_option varchar(50)
)
insert into student
(
student_id,
name
)values(1,'John'),(2,'Martin')
insert into student_option
(
student_option_id,
student_id,
s_option
)values(1,1,'A'),(2,1,'B'),(3,2,'C'),(4,2,'D')
select * from student
select * from student_option
我希望输出显示为
student_name options
John A, B
Martin C, D
在 MYSQL5.6
中执行此操作的最优化方法是什么提前致谢
这个应该可以。你基本上使用 GROUP_CONCAT function from MySQL 和 GROUP BY name after.
select s.name, group_concat(o.s_option)
from student s
left join student_option o on (s.student_id = o.student_id)
group by s.name;
结果:
name s_option
John B,A
Martin D,C