mysql查询到数组

jpfvwuh4  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(360)

嗨,我所有的sql都是这样的:

SELECT 
       group_id,subject_id, 
       GROUP_CONCAT(value order by group_id, subject_id, q_num SEPARATOR ';') val
FROM `exam_answers` where exam_id=6 group by group_id,subject_id;

输出如下:

-----------------------------------------------------------------------------------------------------------------
-subject_id group_id     val                                                                                    -
-----------------------------------------------------------------------------------------------------------------
-1          1           A;B;B;D;C;C;C;E;D;A;E;E;E;B;D;A;B;B;C;C;D;C;42;361;54;27;   D ,    E,A    ;120;480;0.6  -
-----------------------------------------------------------------------------------------------------------------
-2          1           E;D;C;E;B;D;C;E;A;B;B;D;B;B;A;B;B;A;D;B;D;E;8;8;400;124; B D ,A    ,  C E;12;6;3        -
-----------------------------------------------------------------------------------------------------------------
-3          1           C;B;A;A;C;B;D;A;A;C;E;A;D;A;D;A;E;C;A;C;C;B;156;124;128;4;   D ,  C  , B   ;2;5;24      -
-----------------------------------------------------------------------------------------------------------------
-1          2           A;B;B;D;C;C;C;E;D;A;E;E;E;B;D;A;B;B;C;C;D;C;42;361;54;27;   D ,    E,A    ;120;480;0.6  -
-----------------------------------------------------------------------------------------------------------------
-2          2           D;B;C;A;E;D;B;C;B;E;A;A;E;C;B;A;A;D;B;E;A;C;134;356;145;136;  C  ,A  D , B  E;14;24;14  -
-----------------------------------------------------------------------------------------------------------------
-3          2           B;B;C;D;E;B;A;B;C;D;B;D;B;E;E;E;E;B;B;C;A;C;3;34;53241;134;  C  ,A    ,     ;2;1;34     -
-----------------------------------------------------------------------------------------------------------------
-1          3           C;B;A;C;C;B;E;E;C;C;B;D;B;B;D;A;D;C;D;A;D;C;35;135;124;245; BC  ,A  D ,     ;2;1;3      -
-----------------------------------------------------------------------------------------------------------------
-2          3           B;B;C;D;E;B;A;B;C;D;B;D;B;E;E;E;E;B;B;C;A;C;3;34;53241;134;  C  ,A    ,     ;2;1;34     -
-----------------------------------------------------------------------------------------------------------------
-3          3           C;A;E;A;D;B;C;A;A;A;C;D;C;C;A;E;E;C;B;B;C;D;14;35;123;45;A    ,  C  ,     ;3;1;3        -
-----------------------------------------------------------------------------------------------------------------
-1          4           B;D;C;B;A;D;A;C;A;E;C;A;C;A;C;E;B;B;D;C;A;B;245;3412;1;2; B   ,  C  ,A  D ;12;3;2       -
-----------------------------------------------------------------------------------------------------------------
-2          4           C;B;A;A;C;B;D;A;A;C;E;A;D;A;D;A;E;C;A;C;C;B;156;124;128;4;   D ,  C  , B   ;2;5;24      -
-----------------------------------------------------------------------------------------------------------------
-3          4           E;D;C;E;B;D;C;E;A;B;B;D;B;B;A;B;B;A;D;B;D;E;8;8;400;124; B D ,A    ,  C E;12;6;3        -
-----------------------------------------------------------------------------------------------------------------

我想尝试将这个输出传递给数组,然后像这样访问val列。

$my_array[$group_id][$subject_id]

有可能吗?如果有,我怎样才能做到。

7hiiyaii

7hiiyaii1#

$response = $bdd->query('SELECT...');

$my_array=[];

while ($data = $response->fetch())
{
    $my_array[$data['group_id']][$data['subject_id']] = $data['val'];
}

相关问题