0

请简化对我的问题的解释,假设我做了一个小的 sql 查询来从三个表中选择数据:

SELECT  blockTitre, ChampsType, ChampsNom
FROM form_builder
    LEFT JOIN block_champs
        ON formBuilderBId = blockId
    RIGHT JOIN ajout_champs
        ON ChampsId = formBuilderChId

当我 var_dump 结果时,我得到以下结果:

array (size=6)
  0 => 
    object(stdClass)[8]
      public 'blockTitre' => string 'Misc' (length=4)
      public 'ChampsType' => string 'submit' (length=6)
      public 'ChampsNom' => string 'submit' (length=6)
  1 => 
    object(stdClass)[9]
      public 'blockTitre' => string 'Misc' (length=4)
      public 'ChampsType' => string 'hidden' (length=6)
      public 'ChampsNom' => string 'page' (length=4)
  2 => 
    object(stdClass)[10]
      public 'blockTitre' => string 'Information général' (length=21)
      public 'ChampsType' => string 'text' (length=4)
      public 'ChampsNom' => string 'email' (length=5)
  3 => 
    object(stdClass)[11]
      public 'blockTitre' => string 'Information général' (length=21)
      public 'ChampsType' => string 'text' (length=4)
      public 'ChampsNom' => string 'prenom' (length=6)
  4 => 
    object(stdClass)[12]
      public 'blockTitre' => string 'Information général' (length=21)
      public 'ChampsType' => string 'text' (length=4)
      public 'ChampsNom' => string 'age' (length=3)
  5 => 
    object(stdClass)[13]
      public 'blockTitre' => string 'Misc' (length=4)
      public 'ChampsType' => string 'text' (length=4)
      public 'ChampsNom' => string 'nommm' (length=5)

我想要的是通过blockTitre重新组合结果。

我尝试了 SQL 语句GROUP BY,但它只返回两行(我认为这是逻辑)!

请高手如何让所有行按blockTitre分组 ?

先感谢您。

编辑 :

请我需要得到类似的东西:

 0 =>
       'blockTitre' => string 'Misc' 
            'ChampsType' => string 'submit' 
            'ChampsNom' => string 'submit'
            'ChampsType' => string 'text' 
            'ChampsNom' => string 'nommm' 
            'ChampsType' => string 'hidden' 
            'ChampsNom' => string 'page' 
  1 =>     
       'blockTitre' => string 'Information général' 
            'ChampsType' => string 'text' 
            'ChampsNom' => string 'email' 
            'ChampsType' => string 'text' 
            'ChampsNom' => string 'prenom' 
            'ChampsType' => string 'text' 
            'ChampsNom' => string 'age'
4

2 回答 2

1

GROUP BY正如你描述你的情况一样工作。它只会留下唯一的值:MiscInformation général。它将使用它看到的第一行作为其他列的值。所以确实,你只会得到 2 行。

您正在寻找的输出是什么?通常,您GROUP BY要么仅获取唯一值,要么进行某种操作COUNT

请记住,SQL 只能为您提供“平面”数据,即如下结构:

data = {
    "misc": [{
        "row1",
        "row2"
    }],
    "info": [{
        "row1",
        "row2"
    }]
 }

是必须通过逐行读取结果集自己来完成的事情。

于 2013-01-07T14:58:55.410 回答
0

Have a look at PDO::FETCH_GROUP if you're using PDO.

To fetch your results grouped by blockTitre, try this (assuming you've already executed the query with PDO):

$rows = $result->fetchAll(PDO::FETCH_OBJ | PDO::FETCH_GROUP);

More information can be found in the PHP docs.

Now, you can loop through the groups however you like. Just make sure to

GROUP BY blockTitre

in your query.

于 2013-01-07T15:26:52.910 回答