简体   繁体   中英

How can I output rows with a blank field involving a query using multiple JOINS and GROUP_CONCAT in MySQL?

I am having trouble with either my JOINS or the GROUP_CONCAT function in a MySQL query I am trying to write. I have a database with the following tables and enough sample rows to help someone answer my question:

actor table -

actor_id [PK] | actor_name
--------------+--------------
ryan-reynolds | Ryan Reynolds

actress table -

actress_id [PK]    | actress_name
-------------------+-------------------
blake-lively       | Blake Lively
morena-baccarin    | Morena Baccarin
brianna-hildebrand | Brianna Hildebrand

studio table -

studio_id [PK]        | studio_name
----------------------+----------------------
twentieth-century-fox | Twentieth Century Fox
warner-bros           | Warner Bros.
audiovisual-aval-sgr  | Audiovisual Aval SGR

movie table -

movie_id [PK] | movie_title   | studio_id [FK]        | movie_date 
--------------+---------------+-----------------------+---------------
1             | Deadpool      | twentieth-century-fox | 2016-02-12
2             | Green Lantern | warner-bros           | 2011-06-17
3             | Buried        | audiovisual-aval-sgr  | 2010-10-15

movie_actors table -

actor_id [FK] | movie_id [FK]
--------------+--------------
ryan-reynolds | 1
ryan-reynolds | 2
ryan-reynolds | 3

movie_actresses table -

actress_id [FK]    | movie_id [FK]
-------------------+--------------
blake-lively       | 2
morena-baccarin    | 1
brianna-hildebrand | 1

The MySQL query I am trying to write is to return a list of movies for a specific actor, "Ryan Reynolds", along with the movie's release date, studio and then a concatenation of the actresses that also starred in the movie (if there are any). I have written the following query:

SELECT
    movie_date,
    movie_title,
    studio_name,
    GROUP_CONCAT(actress_name SEPARATOR ", ") AS all_actress_names
FROM
    movie
NATURAL JOIN studio NATURAL JOIN movie_actors NATURAL JOIN movie_actresses NATURAL JOIN actress WHERE actor_id = "ryan-reynolds"
GROUP BY
    movie_id
ORDER BY
    movie_date
DESC

But it doesn't return the movie "Buried" because I don't have any actresses linked to that movie. The ouput is as follows:

movie_date | movie_title   | studio_name           | all_actress_names
-----------+---------------+-----------------------+------------------------------------
2016-02-12 | Deadpool      | Twentieth Century Fox | Morena Baccarin, Brianna Hildebrand
2011-06-17 | Green Lantern | Warner Bros.          | Blake Lively

How can I edit my query to return all the movies? Leaving the "all_actress_names" field blank for "Buried", like so:

movie_date | movie_title   | studio_name           | all_actress_names
-----------+---------------+-----------------------+------------------------------------
2016-02-12 | Deadpool      | Twentieth Century Fox | Morena Baccarin, Brianna Hildebrand
2011-06-17 | Green Lantern | Warner Bros.          | Blake Lively
2010-10-15 | Buried        | Audiovisual Aval SGR  |

I think I may be using the wrong JOINS. But it could also be because GROUP_CONCAT is returning a NULL. I have tried using ISNULL, IFNULL and COALESCE on the "actress_name" field, with no luck though.

  1. First, you need to use left join to link your query to actresses as it is possible to have zero of them.
  2. Second, you need to use IFNULL or COALESCE as you said in order to manage NULL values.

See the following query:

SELECT
    movie_date,
    movie_title,
    studio_name,
    GROUP_CONCAT(IFNULL(actress_name,'') ORDER BY actress_name SEPARATOR ", ") AS all_actress_names
FROM
    movie M
NATURAL JOIN studio S
NATURAL JOIN movie_actors MAM
LEFT JOIN movie_actresses MAW ON MAW.movie_id = M.movie_id
LEFT JOIN actress AW ON MAW.actress_id = AW.actress_id
WHERE 
    actor_id = "ryan-reynolds"
GROUP BY
    M.movie_id
ORDER BY
    movie_date
DESC

SEE DEMO HERE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM