I have to join two tables in PHPMyAdmin. One is called tracks
, the other one is album
I select from album
: album_name, year, album_number. Both tables have track_id
column. I use that code:
SELECT `album_name`,`year`, `album_number`
FROM `album`
JOIN `tracks` ON tracks.track_id=album.track_id
And it returns me the result, only from the album table, it does not join the other table, tracks. Where is my mistake?
[example of that query] https://i.stack.imgur.com/cT93m.png
3
Answers
The first option returns me only columns from the album table, those 3 plus track_id. Nothing from the tracks table.
The second one I already tried and it works, but I don't need to see all the columns of both tables.
you are doing a the join and only selecting three columns, album_name, year,album_number
so the results are of only 3 columns, try adding album.track_id in your select to look like this.
refer here on joins
SELECT
album_name
,year
,album_number
,album.track_idFROM
album
LEFT JOIN
tracks
ON tracks.track_id=album.track_idor if you want to get all the columns in both tables do this, I hope one of this will work
SELECT *
FROM
album
LEFT JOIN
tracks
ON tracks.track_id=album.track_idYou need to put all fields from the tables on SELECT statement
SELECT album.*, tracks.* FROM album JOIN tracks ON tracks.track_id = album.track_id