mysql加入和组

时间:2013-06-15 03:29:24

标签: mysql join group-by

从发布的角度来看,我是这个论坛的新手,正如这个问题可能会显示的那样。我在想要显示数据的方式上遇到了一些问题。

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

使用PHP,确实没有任何困难:

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

    );
}