【发布时间】:2020-04-19 11:19:29
【问题描述】:
您好,我在两张表中使用 leftjoin,一张是ordered_products,另一张是ordered_product_option
Ordered_produts 表:-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_id | name
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
1 Track Pants
2 PT tshirt
ordered_product_options 表:-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_id | ordered_produts_id | name | value_name
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
110 1 size 32
111 1 color yellow
112 2 size 25
我的查询:-
$this->orderProduct->leftjoin('ordered_product_options', 'ordered_products._id', '=', 'ordered_product_options.ordered_products__id')
->join('orders', 'ordered_products.orders__id', '=', 'orders._id')
->select(
'ordered_products._id as _id',
'ordered_products.price as total_amount',
'ordered_products.name as product_name',
'ordered_product_options.name as option name',
'ordered_product_options.value_name as option_value'
)->get()->toArray();
结果:-
431 => array:5 [▼
"_id" => 665
"total_amount" => 300.0
"product_name" => "PT TSHIRT"
"option name" => "Size"
"option_value" => "30"
]
432 => array:5 [▼
"_id" => 665
"total_amount" => 300.0
"product_name" => "PT TSHIRT"
"option name" => "Color"
"option_value" => "Yellow"
]
我想要的结果:-
431 => array:5 [▼
"_id" => 665
"total_amount" => 300.0
"product_name" => "PT TSHIRT"
"option name" => "Size","color"
"option_value" => "30","yellow"
]
请提前帮助谢谢
【问题讨论】:
-
按产品和连接选项数据分组。
标签: php mysql laravel laravel-5