按左连接中的数据分组
group by data in left join
我有两个 table:订单和 product_orders
订单 table 中有一个 id。即:9
和 order_id(作为外键)在 product_orders 中重复了 3 次。所以我想要 product_orders table 中的这三个条目和订单 table 中的一个条目。
我的查询是:
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.
您基本上期望使用 SQL 查询无法获得的结果:您想要一个 分层 结构,具有单个 order
和一组 order_product
对应 order
,而查询 returns 一个 flat table,每个 [= 一行10=]加上不同的信息order_product
。这就是 SQL 的工作方式。
解决问题的一种方法是在 php 中进行某种 post 处理,在获取所有行后,将所有具有相同 order
的行分组部分,并为与该订单相关的 order_product
部分生成一个新数组。
我有两个 table:订单和 product_orders 订单 table 中有一个 id。即:9 和 order_id(作为外键)在 product_orders 中重复了 3 次。所以我想要 product_orders table 中的这三个条目和订单 table 中的一个条目。 我的查询是:
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.
您基本上期望使用 SQL 查询无法获得的结果:您想要一个 分层 结构,具有单个 order
和一组 order_product
对应 order
,而查询 returns 一个 flat table,每个 [= 一行10=]加上不同的信息order_product
。这就是 SQL 的工作方式。
解决问题的一种方法是在 php 中进行某种 post 处理,在获取所有行后,将所有具有相同 order
的行分组部分,并为与该订单相关的 order_product
部分生成一个新数组。