如何提取 MySQL 中的部分 Base64 编码字符串?
How to extract part of a Base64 encoded string in MySQL?
我的数据库中有一个编码字段。在字段上使用 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>';
总结
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;
假设
现在假设问题中给出的示例中年份之前的单斜杠是错字,应该是双斜杠。 (如果事实并非如此,我会更新我的答案。)
有点疯狂但有效
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),"-","/");
我的数据库中有一个编码字段。在字段上使用 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>';
总结
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;
假设
现在假设问题中给出的示例中年份之前的单斜杠是错字,应该是双斜杠。 (如果事实并非如此,我会更新我的答案。)
有点疯狂但有效
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),"-","/");