4

查看这个数组,可以看到只有foo6值与每个父键不同:

Array
(
    [0] => Array
        (
          [foo1] => Apple
          [foo2] => Banana
          [foo3] => Carrots
          [foo4] => Deer
          [foo5] => Feather
          [foo6] => Grapes
        )
    [1] => Array
        (
          [foo1] => Apple
          [foo2] => Banana
          [foo3] => Carrots
          [foo4] => Deer
          [foo5] => Feather
          [foo6] => Heater
        )
    [2] => Array
        (
          [foo1] => Apple
          [foo2] => Banana
          [foo3] => Carrots
          [foo4] => Deer
          [foo5] => Feather
          [foo6] => Quail Eggs
        )
)

查询:

SELECT
  tpp.page_master_properties_style AS foo1,
  tpp.page_master_properties_bg AS foo2,
  tpp.page_master_properties_data AS foo3,
  tpp.page_properties_style AS foo4,
  tpp.page_properties_bg AS foo5,
  tpp.page_properties_data AS foo6,
  tobj.objects_script AS foo6
FROM templates t
  INNER JOIN category tc
    ON t.category_id = tc.category_id
  INNER JOIN page_properties tpp
    ON t.templates_id = tpp.templates_id
  INNER JOIN objects tobj
    ON t.templates_id = tobj.templates_id
WHERE
  t.templates_id = ?

在哪里? = 1

这可能是因为 Table对象有多个条目templates_id

+--------------+----------------+-----------------+
|  objects_id  |  templates_id  |  objects_script |
+--------------+----------------+-----------------+
|       1      |        1       |      Grapes     |
|       2      |        1       |      Heater     |
|       3      |        1       |     Quail Eggs  |
|       4      |        2       |       Milk      |
|       5      |        3       |       Lemon     |
+--------------+----------------+-----------------+

我想知道是否有任何内置的 mySQL 函数可以组合foo6成一个单一的数组,例如实现这样的结果:

Array
(
  [foo1] => Apple
  [foo2] => Banana
  [foo3] => Carrots
  [foo4] => Deer
  [foo5] => Feather
  [foo6] => Array
            (
              [0] => Grapes
              [1] => Heater
              [2] => Quail Eggs
            )
)
4

1 回答 1

4

这看起来像是mysql 库中group_concat的工作:

SELECT
  tpp.page_master_properties_style AS foo1,
  tpp.page_master_properties_bg AS foo2,
  tpp.page_master_properties_data AS foo3,
  tpp.page_properties_style AS foo4,
  tpp.page_properties_bg AS foo5,
  group_concat(tpp.page_properties_data) AS foo6,
  tobj.objects_script AS foo6
FROM templates t
  INNER JOIN category tc
    ON t.category_id = tc.category_id
  INNER JOIN page_properties tpp
    ON t.templates_id = tpp.templates_id
  INNER JOIN objects tobj
    ON t.templates_id = tobj.templates_id
WHERE
  t.templates_id = ?
GROUP BY
  tpp.page_master_properties_style AS foo1,
  tpp.page_master_properties_bg AS foo2,
  tpp.page_master_properties_data AS foo3,
  tpp.page_properties_style AS foo4,
  tpp.page_properties_bg AS foo5

这会将所有行组合到唯一的数据行中,并用逗号分隔它们 - 然后当您遍历数据集时tpp.page_properties_data,您可以轻松地将其分解为一个数组。

于 2012-09-14T10:34:15.053 回答