日期查询函数——DATE_FORMAT
许多人在查询sql的时候对时间格式的值知道该怎么查询,跟大家介绍一个好用的年月日自定义查询的函数
例子1
#查询2021年的数据
select * from 表名 where date_format(时间列名,'%Y')='2021'
例子2
#查询2021年1月的数据
select * from 表名 where date_format(时间列名,'%Y%m')='202101'
例子3
#查询2021年1月10日的数据
select * from 表名 where date_format(时间列名,'%Y%m%d')='20210110'
当然许多日期存储其实是2021-08-06这样,那么我们可以这样自定义写法
#查询2021年1月10日的数据
select * from 表名 where date_format(时间列名,'%Y-%m-%d')='2021-01-10'
非常好用的一个时间函数哦,你学会了吗