Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 145935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:30:43+00:00 2026-05-11T08:30:43+00:00

I have 3 tables. For the purposes of this example I will simplify it

  • 0

I have 3 tables. For the purposes of this example I will simplify it as much as I can.

First table contains movie ids and names, second table contains genre ids and genre names (action, drama, etc). 3rd table stores the genre id associated with each movie, since some movies have more than one. It has 2 columns, genre_id and movie_id. Fairy simple stuff. Im trying to output the list of movies, along with a list of genres associated with each movie.

    SELECT *         FROM movies         LEFT JOIN gen_pairs         ON movies.mov_id = gen_pairs.gen_movieid         LEFT JOIN categories         ON gen_pairs.gen_catid = categories.cat_id      GROUP BY mov_id  

This will obviously output a single genre of each film, even if it has multiple ones in the gen_pairs table. How would I get it to display a comma separated list of genres for each movie, without running a sub-query for each item?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:30:44+00:00Added an answer on May 11, 2026 at 8:30 am

    Your select should build the Cartesian product, so you’ll get output like

    MovieA   GenreA MovieA   GenreB MovieB   GenreA ... 

    But it sounds like you want this instead:

    MovieA   GenreA, GenreB MovieB   GenreA ... 

    MySQL has a GROUP_CONCAT function to do what you want:

    SELECT m.mov_id, GROUP_CONCAT(c.name)        FROM movies m   LEFT JOIN gen_pairs gp ON (m.mov_id = gp.gen_movieid)   LEFT JOIN categories c ON (gp.gen_catid = c.cat_id)   GROUP BY m.mov_id 

    Note the GROUP_CONCAT and GROUP BY.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called invoices with 3 fields (for the purposes of this
I have tables linked by FK, I query on the first table using entity
In my database I have tables that define types for example Table: Publication Types
I have two tables (renamed/refactored for illustrative purposes) with a Many-To-Many relationship in an
I have a huge table that is mainly used for backup and administrative purposes.
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I currently have a table with all the data, using this data i want
There are two tables: table A and table B. They have the same columns
I have a relational database with about 100 tables. Each table has unique, numerical,
I have tables named news and tags_news. Given a news id I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.