【问题标题】:inner join array result内连接数组结果
【发布时间】:2012-09-07 12:15:22
【问题描述】:

查看这个数组,可以看到只有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

这可能是因为表 objects 有多个 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
            )
)

【问题讨论】:

    标签: php mysql pdo inner-join


    【解决方案1】:

    这看起来像是 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 中的所有行组合到唯一的数据行中,并用逗号将它们分隔 - 然后您可以在循环遍历数据集时轻松地将explode 放入一个数组中。

    【讨论】:

    • 谢谢!但这令人困惑,我应该为page_properties_data 创建一个新列吗?
    • @LeandroGarcia 不,一点也不。运行您的查询并在 PHP 中将该单个 Column 分解为它自己的数组(作为每一行的更大数组的元素)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 2023-03-19
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多