900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > mysql数据库date_format函数格式化时间

mysql数据库date_format函数格式化时间

时间:2023-09-18 10:25:41

相关推荐

mysql数据库date_format函数格式化时间

第一种:date_format针对传入的参数进行格式化

(select sum(SL) from T_YLGL_YP_KCBGJLwhere YPBH=a.YPBH and LJSC='0' and SJZT='1' and KCBGLX='2'<if test="dto.KSSJ!=null">AND BGSJ >= date_format(#{dto.KSSJ},'%Y-%m-%d') </if><if test="dto.JSSJ!=null">AND BGSJ <= date_format(#{dto.JSSJ},'%Y-%m-%d') </if>) as RKSL

第二种:date_format针对列名格式化,因为数据库date_format的类型是datetime。

(select sum(ZJ) from T_YLGL_YP_KCBGJLwhere YPBH=a.YPBH and LJSC='0' and SJZT='1' and KCBGLX='1' AND JGYYBH = #{dto.JGYYBH}<if test="dto.KSSJ!=null">AND date_format(BGSJ,'%Y-%m-%d') >= #{dto.KSSJ} </if><if test="dto.JSSJ!=null">AND date_format(BGSJ,'%Y-%m-%d') <= #{dto.JSSJ} </if>) as CKJE ,

达到的效果是传入的参数格式可以是单独的年、月份、日,如下:

select * from 表 where date_format(日期,'%Y-%m-%d')='-04-01' 日期select * from 表 where date_format(日期,'%Y-%m')='-04'月份select * from 表 where date_format(日期,'%Y')='' 年

都可以正常的查询到数据

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。