红豆沙 发表于 2024-4-13 19:15:09

MySQL中怎么匹配年月

一般数据库中给到的时间都是年-月-日形式的,那怎么匹配年-月/的形式?
如2021年8月怎么写(怎么在数据库中查询到关于2021年8月的数据):
法一:使用month()函数和year()函数
select
    字段列表
from 表名
where month(date) = 8 and year(date) = 2021;法二:like占位符
select
    字段列表
from 表名
where date like '2021-08%';# %匹配任意个字符法三:substring()函数
select
    字段列表
from 表名
where substring(date,'-',2)='2021-08';法四:
select
    字段列表
from 表名
where date>='2021-08-01' and date<='2021-08-31';法五:
select
    字段列表
from 表名
where date between '2021-08-01' and '2021-08-31';补充:
MySql查询语句根据年份或月份查询
1. 按年份查询
select 字段名 from表 where year(字段名)='年份';2. 按月份查询:
select 字段名 from 表 where month(字段名)='月份';3. 查本年的某一天(例本年的第6天)
select 字段名 from 表 where dayofyear(字段名)='6';到此这篇关于MySQL中怎么匹配年月的文章就介绍到这了,更多相关mysql匹配年月内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/database/319050btb.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: MySQL中怎么匹配年月