【问题标题】:php query select mysql to column for table grid [duplicate]php查询选择mysql到表格网格的列[重复]
【发布时间】:2018-03-13 16:09:51
【问题描述】:

我有这样的mysql表

| idx|  name  | year | month | value |
| 1  |  foo   | 2017 | 1     | bar1  |
| 2  |  foo   | 2017 | 2     | bar2  |
| 3  |  foo   | 2017 | 3     | bar3  |
| 4  |  foo   | 2017 | 4     | bar4  |

我想查询这样的网格表

| idy |  name  | year | month1  | month2 | month3 | month4 |
| 1   |  foo   | 2017 | bar1    | bar2   | bar3   | bar4   | 

如何做到这一点,非常感谢

我的意思是查询一行到一行,我想像这样填充表格

<table>
    <tr>
       <th>id</th>
       <th>name</th>
       <th>year</th>
       <th>month1</th>
       <th>month2</th>
       <th>month3</th>
       <th>month4</th>
    </tr>
    <tr>
       <td><?php echo $row[id]; ?></td>
       <td><?php echo $row[name]; ?></td>
       <td><?php echo $row[year]; ?></td>
       <td><?php echo $row[month1]; ?></td>
       <td><?php echo $row[month2]; ?></td>
       <td><?php echo $row[month3]; ?></td>
       <td><?php echo $row[month4]; ?></td>
    </tr>
</table>

【问题讨论】:

  • 您的查询在哪里?
  • like select * from table where name='foo' and year=2017 .....blablabla 我不知道该怎么做
  • 这就是您需要的所有查询。
  • 不,先生,我只想选择这样的表格
  • 这种转换称为旋转,这个问题已经在 SO 上被问过并回答了好几次。这两个重复的主题解释了如何在 MySQL 中创建数据透视表。但是,在应用程序中进行旋转可能更有效(在这种特殊情况下是 php)。

标签: php mysql sql pivot


【解决方案1】:

您可以通过几种方法来完成您需要的工作。

第一个是带有 MAX 条件的 group by。

SELECT 
    p.name, 
    p.`year`, 
    MAX(IF(p.`month` = 1, p.value, NULL)) AS month1, 
    MAX(IF(p.`month` = 2, p.value, NULL)) AS month2,
    MAX(IF(p.`month` = 3, p.value, NULL)) AS month3,
    MAX(IF(p.`month` = 4, p.value, NULL)) AS month4
FROM table_name AS p
GROUP BY p.name, p.`year`

另一种方法是使用您想要检索的每个月的连接。

SELECT 
  p.name,
  p.`year`, 
  m1.value AS month1, 
  m2.value AS month2, 
  m3.value AS month3, 
  m4.value AS month4
FROM table_name AS p
LEFT JOIN table_name AS m1
ON m1.name = p.name AND m1.`year` = p.`year` AND m1.`month` = 1
LEFT JOIN table_name AS m2
ON m2.name = p.name AND m2.`year` = p.`year` AND m2.`month` = 2
LEFT JOIN table_name AS m3
ON m3.name = p.name AND m3.`year` = p.`year` AND m3.`month` = 3
LEFT JOIN table_name AS m4
ON m4.name = p.name AND m4.`year` = p.`year` AND m4.`month` = 4
#....
WHERE p.name = 'foo' AND p.`year` = 2017
GROUP BY p.name, p.`year`;

演示:http://sqlfiddle.com/#!9/6de54a/3

两者的结果将是相同的,但是您的 ID 将不再相关,因为您将各个行分组为一行,所以我将由您决定如何显示 ID。


您也可以使用 PHP 转置数据,使用名称和年份的数组键分隔显示。

演示:https://3v4l.org/KvCD5

//SELECT * FROM table_name WHERE name = 'foo' AND `year` = '2017';

$data = [];
foreach($rows as $row) {
  $key = $row['name'] . '_' . $row['year'];
  if(!isset($data[$key])){
     $data[$key] = array(
         'id' => $row['id'],
         'name' => $row['name'],
         'year' => $row['year']
    );
  }
  $data[$key]['month' . $row['month']] = $row['value'];
}

结果:

Array
(
    [foo_2017] => Array
        (
            [id] => 1
            [name] => foo
            [year] => 2017
            [month1] => bar1
            [month2] => bar2
            [month3] => bar3
            [month4] => bar4
        )

)

然后您将遍历转置的数据。

<table>
    <tr>
       <th>id</th>
       <th>name</th>
       <th>year</th>
       <th>month1</th>
       <th>month2</th>
       <th>month3</th>
       <th>month4</th>
    </tr>
<?php foreach($data as $row) { ?>
    <tr>
       <td><?php echo $row['id']; ?></td>
       <td><?php echo $row['name']; ?></td>
       <td><?php echo $row['year']; ?></td>
       <td><?php echo $row['month1']; ?></td>
       <td><?php echo $row['month2']; ?></td>
       <td><?php echo $row['month3']; ?></td>
       <td><?php echo $row['month4']; ?></td>
    </tr>
<?php } ?>
</table>

【讨论】:

  • @fyrye 非常感谢你救了我
  • @Shadow 针对特定用例的次数不限。我回答了 OP 的特定用例,它既正确又有用。您标记为重复的问题没有提供。 GROUP BY id vs GROUP BY name, year 将其标记为重复足以将其他人指向类似的答案,但这个问题也可能适用于其他用例。
  • 是的,如果用户询问如何加入两个表,并且没有重复显示 OP 加入的那些字段,那么有一个特定的用例可以再次回答这个问题。 . 这样一来,所有问题都不会作为重复而关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 2012-12-11
相关资源
最近更新 更多