Mysql提取字符串中的时间

NOCO发布于 分类 Mysql

9天前 有1个用户阅读过

背景

有个项目,MYSQL数据库某表其中一个url字段的字符串具有一定规律,需要单独提取时间,如下

http://seonoco.com/dir-1/dir-x/dir/2016/1013/3.html
http://seonoco.com/dir-2/dir-y/dir/2017/1014/123.html
....
http://seonoco.com/dir-n/dir-z/dir/2017/1014/id.html

获取其中的时间 20171014

可使用命令

replace(substring_index(substring_index(url,'/',-3),'/',2),'/','')

获取其中的时间并且转换为长度为unix时间戳

可使用命令

substring_index(unix_timestamp(replace(substring_index(substring_index(url,'/',-3),'/',2),'/','')),'.',1)

然后,就可以用来排序或者单独保存

要点备注

通过substring_index截取字符串

通过replace替换时间中间的斜线/

通过unix_timestamp转换时间,类似PHP中的strtotime

-- The End --

参考与扩展阅读

mysql 函数substring_index()

本文标题: Mysql提取字符串中的时间

本文地址: https://seonoco.com/blog/mysql-substring_index-time

本文是否有所帮助?
点赞 0
感谢支持
0
多谢反馈
评论 0
打赏

支持微信/支付宝

评论

网友