gpt4 book ai didi

mysql - 按部分字符串分组

转载 作者:行者123 更新时间:2023-11-29 03:57:25 25 4
gpt4 key购买 nike

这是我的表格:

----------+-----------+--------------
45678-sm-w| 18 | Mens clothing
----------+-----------+--------------
45678-sm-b| 5 | Mens clothing
----------+-----------+--------------
2189-L-Wh | 4 | Juniors Clothing
----------+-----------+--------------
2189-L-Bl | 3 | Juniors Clothing
----------+---------- +--------------

这是期望的结果:

----------+-----------+--------------
45678 | 23 | Mens clothing
----------+-----------+--------------
2189 | 7 | Juniors Clothing
----------+-----------+--------------

最佳答案

如果你正在选择数据,那么你可以使用这样的东西:

select SUBSTRING_INDEX(col1, '-', 1) col1, 
sum(col2) Total,
col3
from table1
group by SUBSTRING_INDEX(col1, '-', 1), SUBSTRING_INDEX(col3, ' ', 1)
order by col1 desc

参见 SQL Fiddle With Demo

关于mysql - 按部分字符串分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12865411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com