Join Mysql tables; 1 contains the names, the other contains multiple links to names

That seems straight forward; I think I was over complicating things in the first place.
The first performer should come out first ( as the main performer ) on the list as long as I do not do a resort.

I may have to split the album_performers table into two for side A and side B?

This is my current query:


SELECT * FROM album

 JOIN
 album_performers
 ON
 album.album_ID=album_performers.album_ID

 JOIN
 source
 ON
 source.sou_ID=album.source

 JOIN
 performer
 ON
 performer.per_ID=album_performers.performer_ID

 WHERE album.album_ID = '1' ";

And using print_r($row); my output is:

Array ( [album_id] => 1 [0] => 1 [title] => 1 [1] => 1 [size] => 10 [2] => 10 [source] => 1 [3] => 1 [4] => 1 [performer_id] => 1 [5] => 1 [sou_ID] => 1 [6] => 1 [origin] => C.P. Collection [7] => C.P. Collection [per_ID] => 1 [8] => 1 [full_name] => Luciano Pavarotti [9] => Luciano Pavarotti )
1

As you can see I only have one performer; but if I run the above SQL query in phpmy admin I get:



album_id Identifier

title Track name side A

size Record size

source Source of the record

album_id

performer_id

sou_ID

origin

per_ID

full_name


1 1 10 1 1 1 1 C.P. Collection 1 Luciano Pavarotti
1 1 10 1 1 3 1 C.P. Collection 3 Jose Carreras

Any idea why I can not display all the artists?

I was awake last night thinking about this and have been playing with the code all afternoon!

The database is working well and I am getting what I want but I have a problem with the location on side A and Side B. Whatever I do I only end up with one location as the second overwrites the first. I have tried creating a LocationA and a LocationB but I can only get one $row[‘place’]. Running the code through the SQL on phpmyadmin both locations are being picked up and when displaying the $row array with print_r() they are both there. I can get both locations if I do something like $row[‘27’] and $row[‘29’] but if I add something else to the database later I have to remember to change these two numbers.

This is the output of print_r():

Array
(
    [album_id] => 1
    [0] => 1
    [file_number] => 1111
    [1] => 1111
    [title] => 1
    [2] => 1
    [size] => 10
    [3] => 10
    [source] => 1
    [4] => 1
    [company_ID] => 3
    [5] => 3
    [location_A] => 1
    [6] => 1
    [location_B] => 2
    [7] => 2
    [matrix_A] => IRX456
    [8] => IRX456
    [matrix_B] => IRX789
    [9] => IRX789
    [speed_A] => 76
    [10] => 76
    [speed_B] => 78
    [11] => 78
    [date_A] => 1960-12-04
    [12] => 1960-12-04
    [date_B] => 1959-01-01
    [13] => 1959-01-01
    [catalogue] => 123_r5
    [14] => 123_r5
    [date_aq] => 1975-05-05
    [15] => 1975-05-05
    [purchase] => 5
    [16] => 5
    [est_value] => 10
    [17] => 10
    [sold] => 0
    [18] => 0
    [sou_ID] => 1
    [19] => 1
    [origin] => C.P. Collection
    [20] => C.P. Collection
    [comp_ID] => 3
    [21] => 3
    [name] => Hermes records
    [22] => Hermes records
    [album_ID] => 1
    [23] => 1
    [locationA_ID] => 1
    [24] => 1
    [locationB_ID] => 2
    [25] => 2
    [loc_ID] => 2
    [26] => 1
    [place] => Abbey road
    [27] => New York
    [28] => 2
    [29] => Abbey road
)

This is my query:


SELECT * FROM album

 JOIN
 source
 ON
 source.sou_ID=album.source

 JOIN
 company
 ON
 company.comp_ID=album.company_ID

 JOIN
 album_location
 ON
 album.album_ID=album_location.album_ID

 JOIN
 location
 ON
 location.loc_ID=album_location.locationA_ID

 JOIN
 location AS locB
 ON
 locB.loc_ID=album_location.locationB_ID

 WHERE album.file_number = '1111'

These are my tables:


CREATE TABLE IF NOT EXISTS `album` (
  `album_id` int(5) NOT NULL AUTO_INCREMENT COMMENT 'Identifier',
  `file_number` int(5) NOT NULL,
  `title` int(4) NOT NULL COMMENT 'Track name side A',
  `size` enum('8','10','Other') NOT NULL COMMENT 'Record size',
  `source` int(4) NOT NULL COMMENT 'Source of the record',
  `company_ID` int(4) NOT NULL,
  `location_A` int(4) NOT NULL,
  `location_B` int(4) NOT NULL,
  `matrix_A` varchar(10) NOT NULL,
  `matrix_B` varchar(10) NOT NULL,
  `speed_A` int(6) NOT NULL COMMENT 'Speed side A',
  `speed_B` int(6) NOT NULL COMMENT 'Side B speed',
  `date_A` date NOT NULL COMMENT 'Date side A recorderd',
  `date_B` date NOT NULL COMMENT 'Date side B recorderd',
  `catalogue` varchar(12) NOT NULL COMMENT 'Catalogue number',
  `date_aq` date NOT NULL COMMENT 'Purchase date',
  `purchase` int(6) NOT NULL COMMENT 'Purchase price',
  `est_value` int(6) NOT NULL COMMENT 'Current estimated value',
  `sold` int(6) NOT NULL COMMENT 'Sold price',
  PRIMARY KEY (`album_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


CREATE TABLE IF NOT EXISTS `album_location` (
  `album_ID` int(11) NOT NULL,
  `locationA_ID` int(11) NOT NULL,
  `locationB_ID` int(11) NOT NULL,
  PRIMARY KEY (`album_ID`,`locationA_ID`,`locationB_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `location` (
  `loc_ID` smallint(4) NOT NULL AUTO_INCREMENT,
  `place` varchar(50) NOT NULL DEFAULT 'Place',
  PRIMARY KEY (`loc_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='List of performers' AUTO_INCREMENT=3 ;

that’s a php issue, but it’s due to the fact that you have two columns in the query result with the same name

solution: stop using the dreaded, evil "select star"

list the columns you want explicitly, using column aliases as necessary –

SELECT source.foo
     , company.bar
     , locA.name AS location_A
     , locB.name AS location_B
  FROM album
INNER 
  JOIN source 
    ON source.sou_ID = album.source 
INNER
  JOIN company 
    ON company.comp_ID = album.company_ID
INNER 
  JOIN album_location 
    ON album_location.album_ID = album.album_ID
INNER 
  JOIN location AS locA
    ON locA.loc_ID = album_location.locationA_ID
INNER 
  JOIN location AS locB
    ON locB.loc_ID = album_location.locationB_ID
 WHERE album.file_number  =  '1111'

Looks like I am up and running but need to go back and change some of my variable, column names etc. as they are a bit of a mess.

I also need to modify my data input page and its onto the update page :frowning:

Thank you very much for the help r937