Keith Power
Keith Power

Reputation: 14151

mySQL not getting all records

I have 5 tables that I wish to get data from all linking to a table called pubs.

special_offers has 3 records related to the pub events has 4 records related to the pub

However I am only getting back the first event and special_offers. It does not seem to structure into a multi level array. I have tried join and inner joing, I cant see what might be wrong.

SELECT pubs.*, special_offers.*, `events`.*, vote.* 
  FROM pubs
    Inner Join `events` 
      ON pubs.id = `events`.pubID
    Inner Join special_offers 
      ON pubs.id = special_offers.pubID
    Inner Join vote 
      ON pubs.id = vote.pubID
  WHERE pubs.id = 48

Here is the output of print_r

Array (
 [0] => 48
 [id] => 1 Harveys
 [name] => Harveys
 [sun] => 10am-2.30am
 [31] => 1
 [32] => 4
 [special_dayID] => 4
 [33] => 48
 [pubID] => 48
 [34] => 2.50 Drinks
 [title] => Kodakid
 [35] => All drink 2.50 get them while there hot! or cold!
 [desc] => Kodakid are playing!
 [36] => 1
 [37] => 48
 [38] => 2012-04-30
 [date] => 2012-04-30
 [39] => Kodakid
 [40] => & others
 [subtitle] => & others
 [41] => Kodakid are playing!
 [42] => kodakid.jpg
 [img] => kodakid.jpg
 [43] => 1
 [44] => 48
 [45] => 3
 [price_range] => 3
 [46] => 5
 [atmosphere] => 5
 [47] => 2
 [food] => 2
 [48] => 4
 [service] => 4
 [49] => 3
 [value] => 3
 [50] => Waterford City
 [county] => Waterford City )

Thanks

Upvotes: 1

Views: 151

Answers (1)

Mark
Mark

Reputation: 1137

How are you doing the actual querying? MySQL doesn't support nested arrays or rows. A result set consists of zero or more rows, and each row is one or more numbers, strings, dates, and so on.

What you're doing is asking for a result set consisting of all columns in all four tables. Because you're using INNER join, you're asking MySQL to only return rows that are present in all four tables. Thus an ID in pubs that doesn't have an associated row in events will not be included in the result set, even if it does have rows in the other two.

To do what you're asking, you can either use separate queries (one for events, one for votes, etc...). You can also try using LEFT JOINs instead of INNER JOINs or sub-queries and aggregators like GROUP_CONCAT, but it really depends on what you plan on doing with the information.

Upvotes: 1

Related Questions