0
点赞
收藏
分享

微信扫一扫

SQL常用函数与语法,常见语法本文不写入【长期不定时更新】


EXTRACT函数

  • 作用:将某个字段内容由年月日、时分秒构成的数据可根据需要自定义拆分成单个数据。

​语法:EXTRACT(unit FROM date),date参数是合法的日期表达式。 ​​​​unit参数可以是下列的值:​

  • ​​https://www.w3school.com.cn/sql/func_extract.asp​​

如下所示,create_date_h的数据内容为 2020-03-04 09:30:15
注意:返回的值将0抹去,如下时间为09,则查出的数据为9.
-- 年,结果2020
select EXTRACT(YEAR from create_date_h) from hello
-- 月,结果3
select EXTRACT(MONTH from create_date_h) from hello
-- 日,结果4
select EXTRACT(DAY from create_date_h) from hello
-- 时,结果9
select EXTRACT(HOUR from create_date_h) from hello
-- 分,结果30
select EXTRACT(MINUTE from create_date_h) from hello
-- 秒,结果15
select EXTRACT(SECOND from create_date_h) from hello

MySql时间戳涉及的函数

date_format(date, format) 函数,MySQL日期格式化函数date_format()

unix_timestamp() 函数

str_to_date(str, format) 函数

from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函数from_unixtime

时间转字符串

select date_format(now(), '%Y-%m-%d');  

#结果:2016-01-05

时间转时间戳

select unix_timestamp(now());  

#结果:1452001082

字符串转时间

select str_to_date('2016-01-02', '%Y-%m-%d %H');  

#结果:2016-01-02 00:00:00

字符串转时间戳

select unix_timestamp('2016-01-02');  

#结果:1451664000

时间戳转时间

select from_unixtime(1451997924);  

#结果:2016-01-05 20:45:24

时间戳转字符串

select from_unixtime(1451997924,'%Y-%d');  

结果:2016-01-05 20:45:24

mybatis mapper文件占位符自动生成

我们在开发中经常会用到mapper文件的占位符,需要将属性一一进行对应,如果遇到表中字段特别多的时候,那么就非常的折磨人了。现在使用一条SQL,可以自动生成表字段的占位符拼接。

例如:#{item.picture_url}

SQL:
select GROUP_CONCAT(LOWER(column_name) separator '},#{item.') from information_schema.columns where table_name ="campain_picture";

生成结果:
id},#{item.campain_id},#{item.picture_url},#{item.picture_id},#{item.create_date},#{item.update_date

需要我们自行补缺第一个字段的前缀与最后一个字段的后缀即可

#{item.id},#{item.campain_id},#{item.picture_url},#{item.picture_id},#{item.create_date},#{item.update_date}

本文不定时更新,感谢关注!

如何解决了您的问题,还希望来JAVA WEB开发交流群:958923746,有问题欢迎共享,共同提升!

举报

相关推荐

0 条评论