mysql一对多表联接
我有三张桌子,点菜,点菜 订单表包含客户名称、地址等订单信息 ordered_dish表包含ordered_id和dish_id,每个ordered_dish_id都有一个唯一的。ordered_dish_id存在是因为同一道菜+dish_选项可以有不同的组合。以及订购的相同组合的数量 ordered_dish_options包含ordered_id、ordered_dish_id fk到ordered_dish.ordered_dish_id、dish_option_id 注意:在本例中,有两个dish_id=3的菜肴,但dish+dish_选项组合不同 我有以下疑问:mysql一对多表联接,mysql,join,one-to-many,Mysql,Join,One To Many,我有三张桌子,点菜,点菜 订单表包含客户名称、地址等订单信息 ordered_dish表包含ordered_id和dish_id,每个ordered_dish_id都有一个唯一的。ordered_dish_id存在是因为同一道菜+dish_选项可以有不同的组合。以及订购的相同组合的数量 ordered_dish_options包含ordered_id、ordered_dish_id fk到ordered_dish.ordered_dish_id、dish_option_id 注意:在本例中,有两
"SELECT orders.*,
GROUP_CONCAT(ordered_dishes.dish_id SEPARATOR ', ') dish_ids,
GROUP_CONCAT(ordered_dishes.amount SEPARATOR ', ') dish_amounts,
GROUP_CONCAT(ordered_dishes.ordered_dish_id SEPARATOR ', ') dish_odi,
GROUP_CONCAT(ordered_dish_options.ordered_dish_id SEPARATOR ', ') do_odi,
GROUP_CONCAT(ordered_dish_options.dish_option_id SEPARATOR ', ') dish_option_ids
FROM orders
LEFT JOIN ordered_dishes ON orders.order_id=ordered_dishes.order_id
LEFT JOIN ordered_dish_options ON orders.order_id=ordered_dish_options.order_id
WHERE orders.order_id=1"
此查询提供了以下信息:
order_id,
orderdata,
dish_ids='3,3,6,3,3,6,3,3,6',
dish_amounts='1,2,1,1,2,1,1,2,1',
dish_odi='1,2,3,1,2,3,1,2,3',
do_odi='1,1,1,1,1,1,1,1,1'
dish_option_id='2'
我想要的是所有订单数据、菜名+数量+订购菜名、菜名选项菜名+订购菜名。我想要最后两个表中的订购菜名将菜名选项链接到相应的菜名 我终于得到了一个预期结果的查询: 选择orders.*,ordered_disks.dishids,ordered_disks.amounts,ordered_dish_options.odos,ordered_dish_options.dois 从命令 左连接选择顺序\u id, 一组菜,一组菜,一组菜, 组\u CONCATordered \u菜品数量 从点菜 按订单分组\u id 点菜。点菜id=点菜。点菜id 左连接选择顺序\u id, 组\u CONCATordered \u dish \u options.ordered \u dish \u id odos, 组\u CONCATordered\u dish\u options.dish\u option\u id dois 从订购的\u盘\u选项 按订单分组\u id 订单上的已订购菜肴选项。订单id=已订购菜肴选项。订单id 其中orders.order\u id=6
so编辑器有点糟糕,我无法将查询转换为4个空格的代码格式。一个观察:order\u id在ordered\u dish\u选项中是多余的否,因为对于每个订单,ordered\u dish\u id odi从1开始递增。所以order1有3道菜,有odi1,2,3。例如,order2有4道带有odi1、2、3、4的菜肴。因此,当订购盘选项加入时,它必须在订购盘id上加入,并链接到订购盘的相应odi,以您自己的方式进行连接
order_id,
orderdata,
dish_ids='3,3,6,3,3,6,3,3,6',
dish_amounts='1,2,1,1,2,1,1,2,1',
dish_odi='1,2,3,1,2,3,1,2,3',
do_odi='1,1,1,1,1,1,1,1,1'
dish_option_id='2'