MySQL查询子父关系

时间:2019-06-11 06:41:51

标签: php mysql

我有一个查询

SELECT bs.i_description,col.Column_name,bs.Tm_id,ad.UserName as name,
        a.fk_sprint_id as spid,b.fk_back_id,b.u_pos_is,b.s_id,b.color,b._left,b._top,
        b.wiptime as wiptime,b.dodtime as dodtime,b.Dep_status 
FROM backToSprint a 
JOIN backToSprint b ON a.`s_id` = b.`fk_f_id` 
join backlog bs on bs.b_id=b.fk_back_id 
left join admin ad on bs.Tm_id=ad.adminID 
left join admin_column col on col.column_id=b.u_pos_is
where a.fk_sprint_id=3 and a.teamid=1 
ORDER BY a.`fk_f_id`

我在哪里得到

Array
(
 [0] => Array
    (
        [i_description] => Story 1
        [Column_name] => Backlogs
        [Tm_id] => 0
        [name] => 
        [spid] => 3
        [fk_back_id] => 408
        [u_pos_is] => 1
        [s_id] => 5
        [color] => 2
        [_left] => 18
        [_top] => -9
        [wiptime] => 
        [dodtime] => 
        [Dep_status] => 0
    )

[1] => Array
    (
        [i_description] => Story 2
        [Column_name] => Backlogs
        [Tm_id] => 0
        [name] => 
        [spid] => 3
        [fk_back_id] => 409
        [u_pos_is] => 1
        [s_id] => 6
        [color] => 3
        [_left] => 18
        [_top] => -9
        [wiptime] => 
        [dodtime] => 
        [Dep_status] => 0
    )

)

此处列名相同,因此我希望输出类似

Array
(
  ['c'] => Array
    (
            [Column_name] => Backlogs
    )
 [0] => Array
    (
        [i_description] => Story 1
        [Column_name] => Backlogs
        [Tm_id] => 0
        [name] => 
        [spid] => 3
        [fk_back_id] => 408
        [u_pos_is] => 1
        [s_id] => 5
        [color] => 2
        [_left] => 18
        [_top] => -9
        [wiptime] => 
        [dodtime] => 
        [Dep_status] => 0
    )

[1] => Array
    (
        [i_description] => Story 2
        [Column_name] => Backlogs
        [Tm_id] => 0
        [name] => 
        [spid] => 3
        [fk_back_id] => 409
        [u_pos_is] => 1
        [s_id] => 6
        [color] => 3
        [_left] => 18
        [_top] => -9
        [wiptime] => 
        [dodtime] => 
        [Dep_status] => 0
    )

)

然后我尝试了

$this->view->getstories['c'] = ['Column_name' => array_unique(array_column(  $this->view->getstories, 'Column_name'))];

没有得到实际的输出。我得到了类似的输出

Array
(
    [0] => Array
        (
            [i_description] => Story 1
            [Column_name] => Backlogs
            [Tm_id] => 0
            [name] => 
            [spid] => 3
            [fk_back_id] => 408
            [u_pos_is] => 1
            [s_id] => 5
            [color] => 2
            [_left] => 18
            [_top] => -9
            [wiptime] => 
            [dodtime] => 
            [Dep_status] => 0
        )

    [1] => Array
        (
            [i_description] => Story 2
            [Column_name] => WIP
            [Tm_id] => 0
            [name] => 
            [spid] => 3
            [fk_back_id] => 409
            [u_pos_is] => 2
            [s_id] => 6
            [color] => 3
            [_left] => 18
            [_top] => -9
            [wiptime] => 
            [dodtime] => 
            [Dep_status] => 0
        )

    [c] => Array
        (
            [Column_name] => Array
                (
                    [0] => Backlogs
                    [1] => WIP
                )

        )

)

(这里我还有另一个列名WIP) 但是我想要像这样的嵌套数组

Array
    (




        [c1] => Array
            (
                [Column_name] => Array
                    (
                        [0] => Backlogs

                    )
[0] => Array
            (
                [i_description] => Story 1
                [Column_name] => Backlogs
                [Tm_id] => 0
                [name] => 
                [spid] => 3
                [fk_back_id] => 408
                [u_pos_is] => 1
                [s_id] => 5
                [color] => 2
                [_left] => 18
                [_top] => -9
                [wiptime] => 
                [dodtime] => 
                [Dep_status] => 0
            )
    [c2] => Array
            (
                [Column_name] => Array
                    (
                        [0] => WIP

                    )
[0] => Array
            (
             [i_description] => Story 2
            [Column_name] => WIP
            [Tm_id] => 0
            [name] => 
            [spid] => 3
            [fk_back_id] => 409
            [u_pos_is] => 2
            [s_id] => 6
            [color] => 3
            [_left] => 18
            [_top] => -9
            [wiptime] => 
            [dodtime] => 
            [Dep_status] => 0
            )

            )

    ) 

请帮助我解决此问题  任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您想要的是嵌套数组。在使用$row['Column_name']作为主数组的键从查询中获取行的同时,将每一行推入该嵌套数组中。

$results = [];
while ($row = $stmt->fetch()) {
    $results[$row['Column_name']][] = $row;
}

这将产生如下结果:

Array
(
    [Backlogs] => Array
    (
        [0] => Array
        (
            [i_description] => Story 1
            [Column_name] => Backlogs
            [Tm_id] => 0
            [name] => 
            [spid] => 3
            [fk_back_id] => 408
            [u_pos_is] => 1
            [s_id] => 5
            [color] => 2
            [_left] => 18
            [_top] => -9
            [wiptime] => 
            [dodtime] => 
            [Dep_status] => 0
        )
    )
    [WIP] => Array
    (
        [0] => Array
        (
            [i_description] => Story 2
            [Column_name] => WIP
            [Tm_id] => 0
            [name] => 
            [spid] => 3
            [fk_back_id] => 409
            [u_pos_is] => 2
            [s_id] => 6
            [color] => 3
            [_left] => 18
            [_top] => -9
            [wiptime] => 
            [dodtime] => 
            [Dep_status] => 0
            )
        )
    )
)

不需要c1c2键,只需使用Column_name值作为主数组的键即可。