我在获取 MySQL 小计时遇到问题。
源数据
网址计数
/test/a/1/temp.txt 10
/test/a/2/temp.txt 10
/test/a/3/temp.txt 10
/test/b/1/temp.txt 10
/test/1/temp.txt 10
/test/1/temp2.txt 10
/test/1/temp3.txt 10
结果
网址计数级别
/test 70 1
/test/a 30 2
/test/a/1 10 3
/test/a/1/temp.txt 10 4
/test/a/2 10 3
/test/a/2/temp.txt 10 4
/test/a/3 10 3
/test/a/3/temp.txt 10 4
/test/b 40 2
/test/b/1 10 3
/test/b/1/temp.txt 10 4
/test/1 30 2
/test/1/temp.txt 10 3
/test/1/temp2.txt 10 3
/test/1/temp3.txt 10 3
我想知道怎么做。
谢谢 谨致问候。
最佳答案
对于您的特定数据,您可以执行以下操作:
select url, sum(count(*)
from ((select substring_index(url, '/', 2) as url, count
from source
) union all
(select substring_index(url, '/', 3) as url, count
from source
) union all
(select substring_index(url, '/', 4) as url, count
from source
) union all
(select substring_index(url, '/', 5) as url, count
from source
)
) s
group by url
order by url;
关于mysql - 关于 MySQL 子总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43970085/