MySQL - 嵌套结果串联
MySQL - nested result concatenation
我有 3 个表(来自 sakila 示例数据库):电影、film_actor 和演员。
Film: (1000 rows)
film_id | title | ....
______________________
1 | a | ....
2 | b | ....
3 | c | ....
4 | d | ....
5 | e | ....
......................
Film_actor:(5000+ rows)
actor_id | film_id | ....
______________________
1 | 12 | ....
1 | 33 | ....
2 | 210 | ....
3 | 2 | ....
3 | 500 | ....
......................
Actor:(~200 rows)
actor_id | first_name | last_name
__________________________________
1 | a | f
2 | b | g
3 | c | h
4 | d | i
5 | e | l
...............................
有没有办法得到如下所示的结果:
title(film) | actors(first_name, last_name)
_________________________________________________________________________
title 1 | first_name1 last_name1, first_name2 last_name2, etc..
title 2 | first_name1 last_name1, first_name2 last_name2, etc..
title 3 | first_name1 last_name1, first_name2 last_name2, etc..
title 4 | first_name1 last_name1, first_name2 last_name2, etc..
title 5 | first_name1 last_name1, first_name2 last_name2, etc..
.........................................................................
即获取按 ID 排序的电影列表,将所有在电影中扮演的演员放入第 2 列。
我已经检查过,但由于嵌套连接,Stack Overflow 上似乎没有答案符合我的问题。
我只需要通过 MySQL 查询得到这个结果。
select `title`, group_concat(concat(first_name, ' ', last_name)) as actors
from film inner join film_actor on film.film_id = film_actor.`film_id`
inner join actor on film_actor.actor_id = actor.actor_id
group by `title`
我有 3 个表(来自 sakila 示例数据库):电影、film_actor 和演员。
Film: (1000 rows) film_id | title | .... ______________________ 1 | a | .... 2 | b | .... 3 | c | .... 4 | d | .... 5 | e | .... ......................
Film_actor:(5000+ rows) actor_id | film_id | .... ______________________ 1 | 12 | .... 1 | 33 | .... 2 | 210 | .... 3 | 2 | .... 3 | 500 | .... ......................
Actor:(~200 rows) actor_id | first_name | last_name __________________________________ 1 | a | f 2 | b | g 3 | c | h 4 | d | i 5 | e | l ...............................
有没有办法得到如下所示的结果:
title(film) | actors(first_name, last_name) _________________________________________________________________________ title 1 | first_name1 last_name1, first_name2 last_name2, etc.. title 2 | first_name1 last_name1, first_name2 last_name2, etc.. title 3 | first_name1 last_name1, first_name2 last_name2, etc.. title 4 | first_name1 last_name1, first_name2 last_name2, etc.. title 5 | first_name1 last_name1, first_name2 last_name2, etc.. .........................................................................
即获取按 ID 排序的电影列表,将所有在电影中扮演的演员放入第 2 列。
我已经检查过,但由于嵌套连接,Stack Overflow 上似乎没有答案符合我的问题。
我只需要通过 MySQL 查询得到这个结果。
select `title`, group_concat(concat(first_name, ' ', last_name)) as actors
from film inner join film_actor on film.film_id = film_actor.`film_id`
inner join actor on film_actor.actor_id = actor.actor_id
group by `title`