MySQl 合并条件为真的行

发布于 2024-11-05 15:45:27 字数 1650 浏览 2 评论 0原文

我花了几天时间试图解决这个问题,但收效甚微,我确信有一个简单的答案,但尽管进行了大量研究,但到目前为止还没有运气,我担心问题是我有限的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您可以按 order_products 表的唯一键进行分组。由于我不确定那是什么,所以我只是按 Select 子句中的所有列进行分组,而不是使用 Group_Concat 的列。

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
    , Group_Concat( Concat(patt.products_options_id, '-', patt.products_options_values_id) ) As optioncodes
    , Group_Concat( 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
Group By oi.orders_id, oi.products_id
    , oi.product_quantity, oi.final_price, oi.products_name
Order By itemnumber

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.

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
    , Group_Concat( Concat(patt.products_options_id, '-', patt.products_options_values_id) ) As optioncodes
    , Group_Concat( 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
Group By oi.orders_id, oi.products_id
    , oi.product_quantity, oi.final_price, oi.products_name
Order By itemnumber
你的心境我的脸 2024-11-12 15:45:27

您应该能够像您所说的那样使用GROUP_CONCAT,但为此,您还必须GROUP BY ItemNumber

You should be able to use GROUP_CONCAT like you said, but in order to do so, you must also GROUP BY ItemNumber.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文