Jump to content

Recommended Posts

I don't use this as much as I should, and when I do I really seem to struggle with it.

 

I'm creating a simple database for a band.  I have a discography section where I want to display the albums from 1 table and the related songs from another.

 

The ALBUMS table has the following fields

id (primary key)

title

year

 

and the ALBUM_SONGS has the following fields

id (primary key)

album_id

track_number

title

 

How do I pull out each album and the related songs to that album in 1 SQL query?

 

I know it can be done, but like I said, I don't do this that often and my mySQL skills are poor.

 

Also not knowing exactly how the array comes back, any help with displaying the information in PHP would be a big help. 

 

Really where I struggle is the MySQL though.  This forum has been a great resource, and I thank everyone for the continued help / support.

ahh yes.

 

SELECT albums.title, albums.date, albums.image_type, album_songs.id, album_songs.album_id, album_songs.song_number, album_songs.song_title FROM albums, album_songs WHERE albums.id=album_songs.album_id ORDER BY albums.date DESC, album_songs.song_number ASC

 

good stuff.

 

I need to brush up on my MySQL.  Thanks for the resource, but I thought it would be a little harder than that.  I guess not.  A little reading never hurt anyone.

I noticed by using "a" and "as" as aliases, it cause sql syntax errors.  So I switched them up.

 

SELECT * from albums al INNER JOIN album_songs als ON al.id = als.album_id ORDER BY al.date DESC, als.song_number ASC";

 

thank you.

This is working great, but I'm now running into an issue where if there are no songs available for listing on the album, the album itself will not display because there are no associated songs.  Is there a way to display the albums with no associated songs in the database without an additional query?

 

Seems like I would do a LEFT JOIN, but that extra album with no matching songs is not displaying.

Ahh yes, that does work, the left join that is.

 

It was more an error in my MySQL Table configuration.  I need to be sure not to use the same name for fields in relational databases.

 

Learning...  ;)

Yeah, I ran into problems using fields with the same name in relational database such as "id"... need to start making that more unique across multiple relational databases... just makes it easier.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.