M
M
m1kz2021-09-24 14:37:45
MySQL
m1kz, 2021-09-24 14:37:45

How to link many entities to one as a result?

There is a movie table with a unique id. There are comments from their unique id, film_id is the film's unique key and comment text. How to display all comments under Movie field name?

For example:

|Название фильма.Текст|  Название фильма.Ключ  |
 |-------------------------|----------------------------|
 |Комментарий один        | Ключ комм   1        |
 |-------------------------|----------------------------|
 |Комментарий два          | Ключ комм  2          |

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akina, 2021-09-24
@Akina

SELECT for_output
FROM ( SELECT name AS film_name, name AS for_output
       FROM films
     UNION ALL
       SELECT films.name, comments.comment
       FROM films 
       JOIN comments USING (film_id) ) total
ORDER BY film_name, film_name <> for_output

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question