1

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

所以,我有 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 列的表格,其中每个州都列出了中心,但我真的在寻找一行来显示中心,然后是所有州,并计算该中心和下一个中心。

任何援助将不胜感激。

4

1 回答 1

0

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

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

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

    );
}
于 2013-06-15T08:16:52.207 回答