按左连接中的数据分组

时间:2015-06-27 19:29:00

标签: php mysql sql left-join

我有两个表:orders和product_orders 订单表中有一个ID。即:9 和product_orders中的order_id(作为外键)重复3次。所以我希望在product_orders表中有三个条目,在orders表中有一个条目。 我的疑问是:

SELECT orders.*,order_products.* FROM orders LEFT JOIN order_products ON orders.id=order_products.order_id

这给出了:

    Array
(
    [0] => Array
        (
            [orders] => Array
                (
                    [id] => 9
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 2
                    [location_id] => 5
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-27 12:49:34
                    [modified] => 2015-06-27 12:49:34
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 2
                    [order_id] => 9
                    [product_id] => 1
                    [pieces] => 1
                )

        )

    [1] => Array
        (
            [orders] => Array
                (
                    [id] => 9
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 2
                    [location_id] => 5
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-27 12:49:34
                    [modified] => 2015-06-27 12:49:34
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 3
                    [order_id] => 9
                    [product_id] => 2
                    [pieces] => 1
                )

        )

    [2] => Array
        (
            [orders] => Array
                (
                    [id] => 9
                    [name] => Abdus Sattar Bhuiyan
                    [email] => sattar.kuet@gmail.com
                    [mobile] => 01673050495
                    [alt_mobile] => 01818953250
                    [city_id] => 2
                    [location_id] => 5
                    [status] => No contact
                    [chashed] => NO
                    [created] => 2015-06-27 12:49:34
                    [modified] => 2015-06-27 12:49:34
                    [comment] => 
                )

            [order_products] => Array
                (
                    [id] => 4
                    [order_id] => 9
                    [product_id] => 3
                    [pieces] => 1
                )

        )

)

我的预期结果是:

Array
    (
        [0] => Array
            (
                [orders] => Array
                    (
                        [id] => 9
                        [name] => Abdus Sattar Bhuiyan
                        [email] => sattar.kuet@gmail.com
                        [mobile] => 01673050495
                        [alt_mobile] => 01818953250
                        [city_id] => 2
                        [location_id] => 5
                        [status] => No contact
                        [chashed] => NO
                        [created] => 2015-06-27 12:49:34
                        [modified] => 2015-06-27 12:49:34
                        [comment] => 
                    )

                [order_products] => Array
                    (
                          [0] => Array(
                                   [id] => 2
                                   [order_id] => 9
                                   [product_id] => 1
                                   [pieces] => 1
                                )
                           [1] => Array(
                                   [id] => 3
                                   [order_id] => 9
                                   [product_id] => 2
                                   [pieces] => 1
                                )   
                             [2] => Array(
                                  [id] => 4
                                  [order_id] => 9
                                  [product_id] => 3
                                  [pieces] => 1
                                ) 

                    )

            )
    )

    I used GROUP BY orders.id. But no luck.  

1 个答案:

答案 0 :(得分:2)

您基本上期望使用SQL查询无法获得结果:您需要一个层次结构结构,只有一个order和一组order_product对于order,查询返回 flat 表,每个order一行,以及另一个order_product的信息。这就是SQL的工作方式。

解决问题的一种方法是在获取所有行之后在php中进行一种后处理,将所有行组合在一起,并为order部分生成一个新数组{1}}与该订单相关的部分。