1

我有 5 个表,我希望从所有链接到名为 pubs 的表中获取数据。

special_offers 有 3 条与 pub 相关的记录 events 有 4 条与 pub 相关的记录

但是,我只取回了第一个事件和 special_offers。它似乎没有构造成多级数组。我试过加入和内部加入,我看不出有什么问题。

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

这是 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 )

谢谢

4

1 回答 1

1

您如何进行实际查询?MySQL 不支持嵌套数组或行。结果集由零或多行组成,每一行是一个或多个数字、字符串、日期等。

您正在做的是要求一个由所有四个表中的所有列组成的结果集。因为您使用的是 INNER 连接,所以您要求 MySQL 仅返回所有四个表中都存在的行。因此pubs,没有关联行的 IDevents不会包含在结果集中,即使它在其他两个中确实有行。

要执行您的要求,您可以使用单独的查询(一个用于事件,一个用于投票等)。您也可以尝试使用 LEFT JOINs 而不是 INNER JOINs 或子查询和聚合器,如 GROUP_CONCAT,但这实际上取决于您打算如何处理这些信息。

于 2012-04-20T16:13:32.740 回答