2

我有以下 MySQL 数据库:

    | id         | sessionId   | time                |
    |------------|-------------|---------------------|
    | 54         |           4 | 2013-03-23 09:00:00 |
    | 55         |           4 | 2013-03-23 09:15:00 |
    | 56         |           5 | 2013-04-20 01:00:00 |
    | 57         |           5 | 2013-04-20 15:15:00 |
    | 58         |           5 | 2013-04-20 19:15:00 |

我试图这样返回一个数组:

    Array
    (
        [0] => Array
            (
                [sessionId] => 4
                [time] => Array
                    (
                        [0] => 2013-03-23 09:00:00
                        [1] => 2013-03-23 09:15:00
                    )

            )

        [1] => Array
            (
                [sessionId] => 5
                [time] => Array
                    (
                        [0] => 2013-04-20 01:00:00
                        [1] => 2013-04-20 15:15:00
                        [2] => 2013-04-20 19:15:00
                    )

            )

    )

到目前为止运行此查询

    $result = mysql_query("SELECT * FROM `Exercise_Lists_New` WHERE `patientId` = 381");
    $row = mysql_fetch_assoc($result);
    while($row = mysql_fetch_array($result)){
        $sessionIds[] = array("sessionId" =>$row['sessionId'], "time" => array($row['time']));
    }
    print_r($sessionIds,true);

给我以下

    Array
        (
            [0] => Array
                (
                    [sessionId] => 4
                    [time] => Array
                        (
                            [0] => 2013-03-23 09:00:00
                        )

                )

            [1] => Array
                (
                    [sessionId] => 4
                    [time] => Array
                        (
                            [0] => 2013-03-23 09:15:00
                        )

                )

            [2] => Array
                (
                    [sessionId] => 5
                    [time] => Array
                        (
                            [0] => 2013-04-20 01:00:00
                        )

                )

            [3] => Array
                (
                    [sessionId] => 5
                    [time] => Array
                        (
                            [0] => 2013-04-20 15:15:00
                        )

                )

            [4] => Array
                (
                    [sessionId] => 5
                    [time] => Array
                        (
                            [0] => 2013-04-20 19:15:00
                        )

                )

        )

所以我想将所有共享相同“sessionId”的“时间”组合在一起。使用 php.ini 执行此操作的最佳方法是什么?

谢谢

4

4 回答 4

2
SELECT
    sessionId,
    GROUP_CONCAT(time) AS `Times`
FROM mytable
GROUP BY    sessionId

您可以为此使用 mysql 的 group_concat。这将为您提供逗号分隔字段的结果,您可以使用 php 展开该字段以获取数组。

输出 :

| sessionId   | time                                                           |
|-------------|--------------------------------------------------------------- |
|           4 | 2013-03-23 09:00:00 , 2013-03-23 09:15:00                      |
|           5 | 2013-04-20 01:00:00 , 2013-04-20 15:15:00 , 2013-04-20 19:15:00|

编辑:

$query  =   "   SELECT
                    sessionId,
                    GROUP_CONCAT(time) AS `Times`
                FROM mytable
                GROUP BY    sessionId";

$result = mysqli_query($query);
$row = mysqli_fetch_assoc($result);
$result_array   =   array();
$i=0;
while($row = mysql_fetch_array($result)){
    $result_array[$i]['sessionId']  = $row['sessionId'];
    $result_array[$i]['time']       = explode(',',$row['Times']);
    $i++;
}
print_r($result_array,true);

结果

Array
    (
        [0] => Array
            (
                [sessionId] => 4
                [time] => Array
                    (
                        [0] => 2013-03-23 09:00:00
                        [1] => 2013-03-23 09:15:00
                    )

            )
        [1] => Array
            (
                [sessionId] => 5
                [time] => Array
                    (
                        [0] => 2013-04-20 01:00:00
                        [1] => 2013-04-20 15:15:00
                        [2] => 2013-04-20 19:15:00
                    )

            )
    )   
于 2013-03-20T14:12:19.277 回答
2

只需尝试:

while($row = mysql_fetch_array($result)){
    if ( !isset($sessionIds[$row['sessionId']]) ) {
        $sessionIds[$row['sessionId']] = array(
            'sessionId' => $row['sessionId'],
            'time'      => array()
        );
    }
    $sessionIds[$row['sessionId']]['time'][] = $row['time'];
}
于 2013-03-20T14:14:30.837 回答
0

从该数组中剥离一层,并使用 sessionID 作为键,例如

$sessionIDs = array();
while($row = mysql_fetch_array($result)){
    $sessionIds[$row['sessionID']][] = $row['time'];
}

这会给你类似的东西:

$arr = array(
     'id' => array(time1, time2, time3, ...),
     'id' => arraY(...)
     etc..
);

请注意,您在代码中丢失了一行,因为您在循环外进行调用,然后立即用 foreach 循环中的 fetch 调用fetch()覆盖它。$row

于 2013-03-20T14:12:56.313 回答
0

试试这个

<?php

$result = mysql_query("SELECT * FROM `Exercise_Lists_New` WHERE `patientId` = 381");
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_array($result)){    
    $sessionIds[$row['sessionId']]['sessionId'] = $row['sessionId'];
    $sessionIds[$row['sessionId']]['time'][] = $row['time'];
}

print_r($sessionIds):

Array
   (
       [4] => Array
           (
               [sessionId] => 4
               [time] => Array
                   (
                       [0] => 2013-03-23 09:00:00
                       [1] => 2013-03-23 09:15:00
                   )

           )

       [5] => Array
           (
               [sessionId] => 5
               [time] => Array
                   (
                       [0] => 2013-04-20 01:00:00
                       [1] => 2013-04-20 15:15:00
                       [2] => 2013-04-20 19:15:00
                   )
           )

   )
于 2013-03-20T14:16:07.780 回答