MySQl 合并条件为真的行
我花了几天时间试图解决这个问题,但收效甚微,我确信有一个简单的答案,但尽管进行了大量研究,但到目前为止还没有运气,我担心问题是我有限的 SQL 知识。
我有3张桌子。订单、订单产品和订单产品属性。
我试图返回给定订单的结果表,显示所有匹配的订单产品,并合并该产品存在的订单产品属性行(有时没有属性),
我必须在这里:
SELECT oi.orders_id AS orderid
, oi.products_id AS ItemNumber
, oi.products_quantity AS Quantity
, oi.final_price AS CostPerUnit
, oi.products_name AS ItemTitle
, concat (patt.products_options_id, '-', patt.products_options_values_id) AS OptionCodes
, concat (patt.products_options, ': ', patt.products_options_values) AS OptionNames
FROM orders_products oi
JOIN orders o
ON o.orders_id = oi.orders_id
LEFT JOIN orders_products_attributes patt
ON patt.orders_products_id = oi.orders_products_id AND patt.orders_id = oi.orders_id
WHERE o.orders_status =2 AND oi.orders_id =10346
ORDER BY ItemNumber
返回以下内容结果:
orderid ItemNumber Quantity CostPerUnit ItemTitle OptionCodes OptionNames
10346 140 1 319.9982 Item 1 3-5 Choice: A
10346 140 1 319.9982 Item 1 1-1 Choice2: B
10346 210 1 112.5000 Item 5 NULL NULL
我试图合并 ItemNumber 相同的行,连接它们存在的 OptionCodes 和 OptionNames,最终得到:
orderid ItemNumber Quantity CostPerUnit ItemTitle OptionCodes OptionNames
10346 140 1 319.9982 Item 1 3-5, 1-1 Choice: A, Choice2: B
10346 210 1 112.5000 Item 5 NULL NULL
我一直在尝试 GROUP_CONCAT 这些但只成功合并所有 3 行。
抱歉,如果我动作缓慢,但我似乎无法从这里继续前进。
TIA, 安德鲁
I've spent a couple of days trying to solve this with limited success, I'm sure there's a simple answer but with no luck so far despite quite a lot of research, I fear the problem is my limited SQL knowledge.
I have 3 tables. orders, orders_products and orders_products_attributes.
I'm trying to return a results table for a given order, showing all orders_products that match, and merging in orders_products_attributes rows for that product where it exists (sometimes there are no attributes)
I've got to here:
SELECT oi.orders_id AS orderid
, oi.products_id AS ItemNumber
, oi.products_quantity AS Quantity
, oi.final_price AS CostPerUnit
, oi.products_name AS ItemTitle
, concat (patt.products_options_id, '-', patt.products_options_values_id) AS OptionCodes
, concat (patt.products_options, ': ', patt.products_options_values) AS OptionNames
FROM orders_products oi
JOIN orders o
ON o.orders_id = oi.orders_id
LEFT JOIN orders_products_attributes patt
ON patt.orders_products_id = oi.orders_products_id AND patt.orders_id = oi.orders_id
WHERE o.orders_status =2 AND oi.orders_id =10346
ORDER BY ItemNumber
Which is returning the following results:
orderid ItemNumber Quantity CostPerUnit ItemTitle OptionCodes OptionNames
10346 140 1 319.9982 Item 1 3-5 Choice: A
10346 140 1 319.9982 Item 1 1-1 Choice2: B
10346 210 1 112.5000 Item 5 NULL NULL
I'm trying to merge rows where ItemNumber is the same, concat'ing OptionCodes and OptionNames where they exist to end up with:
orderid ItemNumber Quantity CostPerUnit ItemTitle OptionCodes OptionNames
10346 140 1 319.9982 Item 1 3-5, 1-1 Choice: A, Choice2: B
10346 210 1 112.5000 Item 5 NULL NULL
I've been trying to GROUP_CONCAT these but only succeed in merging all 3 rows.
Sorry if I'm being slow, but I just can't seem to move on from here.
TIA,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以按
order_products
表的唯一键进行分组。由于我不确定那是什么,所以我只是按 Select 子句中的所有列进行分组,而不是使用 Group_Concat 的列。You could group by the unique key of the
order_products
table. Since I wasn't sure what that was, I just grouped by all the columns in the Select clause other than the ones on which I use Group_Concat.您应该能够像您所说的那样使用
GROUP_CONCAT
,但为此,您还必须GROUP BY ItemNumber
。You should be able to use
GROUP_CONCAT
like you said, but in order to do so, you must alsoGROUP BY ItemNumber
.