好的,我有一个表名ORDERS,我在订单表中有一个名为OrderedItems的列
在订购的商品中,数据如下所示
Flavor Name (10ml, 0mg nic)
Flavor Name (15ml, 12mg nic)
Flavor Name (15ml with Flavor Boost, 12mg nic)
Flavor Name (30ml, 24mg nic)
Flavor Name (30ml with Flavor Boost, 24mg nic)
我希望能够提取下面的项目并将它们放在一个View中,每个列都有自己的列 味道名称 10/15 / 30ml等...... 0/12 / 24mg等...
我不是最好的写作查询,但如果有人能指出我正确的方向,我可能能够完成它。
有什么建议吗?我是否需要在excel或类似的东西中这样做?
答案 0 :(得分:0)
你想做这样的事吗?
select orderId,
sum(case when flavor_name like 'Chocolate%' then 1 else 0 end) as ChocolateCnt,
sum(case when flavor_name like 'Vanilla%' then 1 else 0 end) as VanillaCnt,
sum(case when flavor_name like 'Rocky Road%' then 1 else 0 end) as RockyRoadCnt
from orders o
group by orderId
答案 1 :(得分:0)
假设 所有 风格的格式相同......
SELECT Substring_index(flavor, ' (', 1) AS flavorName,
Substring(flavor, Locate('(', flavor) + 1,
( Locate('ml', flavor) + 1 ) - Locate('(', flavor)) AS volumne,
Substring(flavor, Locate(', ', flavor) + 2,
Locate(' nic)', flavor) - ( Locate(', ', flavor) + 2 )) AS dose
FROM flavors
<强>结果强>
| FLAVORNAME | VOLUMNE | DOSE | -------------------------------- | Flavor Name | 10ml | 0mg | | Flavor Name | 15ml | 12mg | | Flavor Name | 15ml | 12mg | | Flavor Name | 30ml | 24mg | | Flavor Name | 30ml | 24mg |