【问题标题】:mysql joins and groupsmysql加入和组
【发布时间】:2013-06-15 03:29:24
【问题描述】:

从发帖的角度来看,我是这个论坛的新手,正如这个问题所显示的那样。我希望数据的显示方式存在一些问题。

所以,我有 3 个表(我只显示我想要的列):

访客:

center_id (where the visitor was)  |  state_id (where they came from)

中心:

center_id  |  center

状态:

state_id  |  state

这是我一直在使用的查询

SELECT states.state, visitors.center_id, visitors.state_id, centers.center, COUNT(visitors.state_id) AS totalCount 
FROM visitors 
LEFT JOIN states ON states.state_id = visitors.state_id 
LEFT JOIN centers ON centers.center_id = visitors.center_id 
WHERE visitors.vdate = <some date> AND visitors.state_id <> '0'  
GROUP BY centers.center, visitors.state_id

这会产生以下数组:

Array
(
    [0] => Array
        (
            [state] => Connecticut
            [location_id] => 1
            [state_id] => 8
            [center] => Little River
            [totalCount] => 1
        )

    [1] => Array
        (
            [state] => California
            [location_id] => 5
            [state_id] => 6
            [center] => North Augusta
            [totalCount] => 1
        )

    [2] => Array
        (
            [state] => Colorado
            [location_id] => 5
            [state_id] => 7
            [center] => North Augusta
            [totalCount] => 2
        )
    [6] => Array
        (
            [state] => Connecticut
            [location_id] => 9
            [state_id] => 8
            [center] => Santee
            [totalCount] => 2
        )

     [7] => Array
        (
            [state] => Virginia 
            [location_id] => 9
            [state_id] => 51
            [center] => Santee
            [totalCount] => 1
        )
)

这是我真正想要的:

Array
(
    [Little River] => Array
        (
          [0] => Array
             (
                [state] => Connecticut
                [state_id] => 8
                [totalCount] => 1
             )
        )

    [North Augusta] => Array
        (
           [0] => Array
              (
                [state] => California
                [state_id] => 6
                [totalCount] => 1
              )

           [1] => Array
              (
                [state] => Colorado
                [state_id] => 7
                [totalCount] => 2
              )
          )
      [Santee] => Array
          (
           [0] => Array
              (
                [state] => Connecticut
                [state_id] => 8
                [totalCount] => 2
              )

           [1] => Array
              (
                [state] => Virginia 
                [state_id] => 51
                [totalCount] => 1
              )
          )
)

最终我把它放到一个看起来像这样的表格中:

__________________
|State   |   Count|
-------------------
|     Santee      |
-------------------
| Georgia |   5   |
-------------------
| Alabama |   10  |
-------------------
| North Augusta   |
-------------------
| another |    7  |
-------------------

对不起,啰嗦了,但这是我能描述它的唯一方式。

我也尝试过在 php 中破解它,但我可能也在那里做错了什么。我可以制作一个包含 3 列的表格,其中每个州都列出了中心,但我真的在寻找一行来显示中心,然后是所有州,并计算该中心和下一个中心。

任何帮助将不胜感激。

【问题讨论】:

  • 你是如何在 php 中获取结果的?

标签: mysql join group-by


【解决方案1】:

查询只能返回二维(或更少)维度的结果。您将需要解析此结果集以将其转换为树。

使用 PHP,真的没有什么困难:

foreach ($rawResultSet as $row) {
    $finalResult[$row['center']][] = array(
        $row['state'],
        $row['state_id'],
        $row['totalCount']

    );
}

【讨论】:

  • 感谢您的回复。我确实找到了我正在寻找的东西,它非常接近你推荐的东西。然后是使用嵌套的 foreach 循环的问题。抱歉,我想要 1+,但我没有足够的声望。
猜你喜欢
  • 2021-12-31
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
  • 2012-08-15
  • 2013-03-12
相关资源
最近更新 更多