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

  • Home
  • SEARCH
  • 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 6579503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:54:23+00:00 2026-05-25T15:54:23+00:00

i have many to many database and i’m there is a query that i

  • 0

i have many to many database and i’m there is a query that i just got stuck with and cant do it.

i have 4 tables Artists, Tracks, Albums, Clips

now i’m on the artist page so i need to get them by the artist page, i already got all of them, but not the way i want them.

because some tracks, albums, clips belong to other artists as well (duet) and i need to display their name.

but the problem is that i’m selecting using the artist id so my GROUPC_CONCAT function wont work here is the query that gets the artist albums.

   SELECT  al.album_name, GROUP_CONCAT(a.artist_name SEPARATOR ', ') AS 'artist_name'

   FROM 
     Albums al
   LEFT JOIN 
     ArtistAlbums art ON art.album_id = al.album_id
   LEFT JOIN 
     Artists a on a.artist_id = art.artist_id
   WHERE 
     a.artist_id = 10
   GROUP BY 
     al.album_id

one of the albums have two artists attached to it, but it does not get the other artist name.

when i select by the album_id i get the two artists.

please note that i’m new to mysql and i did not find any answers on this particular problem almost no resources on many-to-many querying.

how can i tackle this problem.?

any resources or books on many-to-many that show how to deal with the database on the application layer will be much appreciated,
thanks in advance.

  • 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. Editorial Team
    Editorial Team
    2026-05-25T15:54:24+00:00Added an answer on May 25, 2026 at 3:54 pm

    Think of table aliases as really being row aliases. That is, for purposes of expressions in the WHERE clause and the select-list, the alias refers to a single row at a time.

    Since you’ve created a condition such that a.artist_id = 10, then only rows for that artist match the condition. What you really want is to match all artists on an album given that one artist is artist_id = 10.

    For that, you need another join, so that the row where artist_id = 10 is matched to all the rows for that respective album.

    SELECT  al.album_name, GROUP_CONCAT(a2.artist_name SEPARATOR ', ') AS `artist_name`
    FROM 
      Albums al
    INNER JOIN 
      ArtistAlbums art ON art.album_id = al.album_id
    INNER JOIN 
      Artists a on a.artist_id = art.artist_id
    INNER JOIN 
      ArtistAlbums art2 ON art2.album_id = al.album_id
    INNER JOIN 
      Artists a2 on a2.artist_id = art2.artist_id
    WHERE 
      a.artist_id = 10
    GROUP BY 
      al.album_id
    

    P.S.: I’ve also replaced your use of LEFT JOIN with INNER JOIN. There’s no reason you needed LEFT JOIN, since you’re explicitly looking for albums that have a matching artist, not all albums whether or not it has artist 10. Review the meaning of different types of join.


    Re your followup question:

    I don’t know of a book specifically about many-to-many queries. I’d recommend books like:

    • SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming, which is my own book and it does cover many-to-many tables.
    • SQL and Relational Theory to understand joins better.
    • Joe Celko’s SQL Programming Style, which imho is Joe Celko’s best book.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables in my database that have a many-to-many relationship, which
I have a database (DB1) with one collation and many tables. I've got another
Is there a limit to how many tables you can have in a database?
I'm working on building a MySQL database that will have many related tables. I
Think Design: I have many applications that share the same user database! Other tables
I have many tables in my database which are interrelated. I have a table
I have many collections of documents in a mongo database that look like this:
I have database with many tables. In the first table, I have a field
I have a database that many different client applications (a smattering of web services,
Many applications have grids that display data from a database table one page at

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.