我有兩個表:訂單和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] => [email protected]
[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] => [email protected]
[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] => [email protected]
[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] => [email protected]
[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.
您正在使用哪些DBMS?和哪種編程語言來獲得您的查詢結果? – Renzo
mysql數據庫和我正在使用php語言 –
您可以使用'array_column()'來聚合它們。看到我的答案在這裏你的相關問題:http://stackoverflow.com/questions/31093087/formatting-an-array-using-php/31094106#31094106 –