I am trying to output data from 2 different tables in my database which are joined by a junction table.
Table 1: musician
Columns: musicianID, surname, fName
Table 2: musician_band
Columns: musicianID, bandID
Table 3: band
Columns: bandID, bandName, genre, yearFormed, origin
Just to start with I want to output:
fName, bandName
I tried using this query:
SELECT DISTINCT fName, bandName
FROM musician_band
JOIN musician ON musician.musicianID = musician_band.musicianID
JOIN band ON band.bandID = band.bandID
But instead of giving me a list of musicians and the bands that they're in, it is instead repeating the first name of the first musician in the databse and then a band name on the right, this is repeated for every band in the database, even where there shouldn't be a relationship between that particular musician and band. Like this:
fname bandName
musician1 band1
musician1 band2
musician1 band3
musician2 band1
musician2 band2
musician2 band3
etc...
How can I correct this so that it only outputs musicians in their correct bands?