mysql groupby基于关系的类似产品(如果有)

olhwl3o2  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(273)

我如何按产品分类查询 transactions 但是我需要有选项的产品分开展示。
例如在表中 transactions 如果我有:

ProductID Quantity Price

    1       1       10
    1       1       10
    1       1       15 (in transaction_options this has option green(price 2,5),yellow(price 2,5))
    1       1       15 (in transaction_options this has option red(price 2,5),blue(price 2,5))

我希望结果是:

ProductID Quantity Price
    1       2       10
    1       1       15 (this has option red(price 2,5),blue(price 2,5))
    1       1       15 (this has option green(price 2,5),yellow(price 2,5))

问题是,他们都有相同的产品标识,价格可以是相同的,但不同的选择。
以下是表格结构: products
id,name,price options
id,name,price transactions
id,product_id,quantity,price transaction_optionsid,transaction_id,option_id (关联表) transactions 以及 optionstransactions 我插入 price 根据选项计算(如果有): product.price+[option.price] = transaction.price

vfhzx4xs

vfhzx4xs1#

Select * from (select col1, col2 from myTable where myOptions is not null) union all (select col1, count(col2) as col2 from myTable where myOptions is null group by col1);

相关问题