6

我的数据库中有一个已编码的字段。在字段上使用 from_base64 后,它看起来像这样:

<string>//<string>//<string>/2017//06//21//<string>//file.txt

路径开头可能有不确定数量的字符串,但是,日期 (YYYY//MM//DD) 将始终在右侧有两个字段(一个字符串后跟文件扩展名)。

我想按这个 YYYY//MM//DD 模式排序,并计算这个日期的所有路径。

所以基本上我想这样做:

select '<YYYY//MM//DD portion of decoded_path>', count(*) from table group by '<YYYY//MM//DD portion of decoded_path>' order by '<YYYY//MM//DD portion of decoded_path>';
4

2 回答 2

5

概括

MySQL 的SUBSTRING_INDEX通过查找指定的分隔符并在指定负计数值时从末尾向后计数来用于执行此操作。

演示

Rextester 演示:http ://rextester.com/TCJ65469

SQL

SELECT datepart, 
       COUNT(*) AS occurrences
FROM
(SELECT CONCAT(
     LEFT(SUBSTRING_INDEX(txt, '//', -5), INSTR(SUBSTRING_INDEX(txt, '//', -5), '//') - 1),
     '/',
     LEFT(SUBSTRING_INDEX(txt, '//', -4), INSTR(SUBSTRING_INDEX(txt, '//', -4), '//') - 1),
     '/',
     LEFT(SUBSTRING_INDEX(txt, '//', -3), INSTR(SUBSTRING_INDEX(txt, '//', -3), '//') - 1))
   AS datepart
 FROM tbl) subq
GROUP BY datepart
ORDER BY datepart;

假设

暂时假设问题中给出的示例中年份之前的单斜杠是错字,应该是双斜杠。(如果事实证明不是这种情况,我会更新我的答案。)

于 2017-06-25T15:03:15.447 回答
1

有点疯狂,但它有效

select REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE('<string>//<string>//<string>/2017//06//21//<string>//file.txt',"//","-"),"/",-1),"-<",1),"-","/"), count(*) from `chaissilist` group by REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE('<string>//<string>//<string>/2017//06//21//<string>//file.txt',"//","-"),"/",-1),"-<",1),"-","/") order by REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE('<string>//<string>//<string>/2017//06//21//<string>//file.txt',"//","-"),"/",-1),"-<",1),"-","/");
于 2017-06-29T08:03:10.733 回答