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 6138253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:51:00+00:00 2026-05-23T17:51:00+00:00

Possible Duplicate: Mysql single query join 3 table and get all the results Hi

  • 0

Possible Duplicate:
Mysql single query join 3 table and get all the results

Hi i want to list all the song for the the album and i want to list all the artist for individual song see below for example.

1. Song Title 1
   - Artist 1, Artist 2, Artist 3    note:(all this individual artist have link to there artist page)
2. Song Title 2
   - Artist 1, Artist 2
3. Song Title 3
   - Artist 1, Artist 2, Artist 3

My tables are song, album, artist, song_artist

song table

+----+-----------+----------+
| id | song_name | album_id |
+----+-----------+----------+
| 1  | Title 1   | 2        |
| 2  | Title 2   | 2        |
| 3  | Title 3   | 2        |
+----+-----------+----------+

album table

+----+------------+
| id | album_name |
+----+------------+
| 1  | Album 1    |
| 2  | Album 2    |
| 3  | Album 3    |
+----+------------+

artist table

+----+-------------+
| id | artist_name |
+----+-------------+
| 1  | Artist 1    |
| 2  | Artist 2    |
| 3  | Artist 3    |
+----+-------------+

song_artist table

+--------+--------------+---------+
| id     | song_is      |artist_id|
+--------+--------------+---------+
| 1      | 1            | 1       |
| 2      | 1            | 5       |
| 3      | 1            | 3       |
| 4      | 2            | 3       |
| 5      | 2            | 1       |
| 6      | 3            | 2       |
| 7      | 3            | 1       |
+--------+--------------+---------+

This my current code.

$id =$_GET['id']; *// Get album id from url*
$query = "SELECT id, song_name, FROM song WHERE album_id = '".$id."'";
$result = mysql_query($query) or die("h".mysql_error());
while( $song = mysql_fetch_assoc($result)){
echo $song['song_name'];
$result1 = mysql_query("SELECT artist.artist_name as artist_name, artist.id as aid
FROM artist
INNER JOIN song_artist
ON artist.id = song_artist.artist_id
WHERE song_artist.song_id = '".$song['id']."' ");
while($row = mysql_fetch_array($result1)){
echo "<a href='".$row['sid']."'>".$row['artist_name']."</a>, ";
}
}

how do i write mysql single query to get all results in php?

Thank you for your help in advance.

James.

Ok Now i got the result what i want, thank for every one.

$query = "select s.id song_id, s.song_name, group_concat(art.artist_name) artname, a.id
        from song s 
        left outer join album a on a.id = s.album_id
        left outer join song_artist sa on sa.song_id = s.id
        left outer join artist art on art.id = sa.artist_id
        WHERE a.id= '".$id."'
        GROUP BY song_id";
  • 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-23T17:51:00+00:00Added an answer on May 23, 2026 at 5:51 pm

    Using the query:

    select s.id song_id, s.song_name, a.album_name, art.artist_name
    from song s 
    left outer join album a on a.id=s.album_id
    left outer join song_artist sa on sa.song_id=s.id
    left outer join artist art on art.id=sa.art_id
    where <INSERT WHERE-CLAUSE HERE> 
    

    You’ll get results like:

    +--------+-----------+------------+-------------+
    | song_id|song_name  | album_name | artist_name |
    +--------+-----------+------------+-------------+
    | 1      | Title 1   | Album 2    | Artist 1    |
    | 1      | Title 1   | Album 2    | Artist 5    |
    | 1      | Title 1   | Album 2    | Artist 3    |
    | 2      | Title 2   | Album 2    | Artist 3    |
    | 2      | Title 2   | Album 2    | Artist 1    |
    | 3      | Title 3   | Album 2    | Artist 2    |
    | 3      | Title 3   | Album 2    | Artist 1    |
    +--------+-----------+------------+-------------+
    

    And will have thus have to iterate over each song id to get the display information for each song from the results. Because you are doing it with one query, you’ll get duplicate song and album names when there are multiple artists for a song, but that’s the price you pay for conciseness.

    UPDATE:

    OR you can use GROUP_CONCAT on artist_name as Eljakim suggested if you won’t use artist information for anything except a concatenated display list.

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

Sidebar

Related Questions

Possible Duplicate: Truncate all tables in a MySQL database in one command? I need
Possible Duplicate: MySQL: FULL OUTER JOIN - How do I merge one column? Real
Possible Duplicate: Alter MYSQL Table To Add Comments on Columns Hi Guys, I googled
Possible Duplicate: PHP MySQL Search And Order By Relevancy Hi, I have a table
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: error: Unable to find vcvarsall.bat I'm trying to install MySql interface for
Possible Duplicate: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result I have
I am writing a query to fetch results for all the values in a
Is it possible using just SQL and MySQL to get the OUTPUT below? SAMPLE
Possible Duplicate: Format mysql datetime with php How to convert the following: 2010-12-07 12:00:00

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.