mySQL 选择下个月到期的记录 [英] mySQL selecting records due within the next month

查看:70
本文介绍了mySQL 选择下个月到期的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择表中日期介于当前日期和未来 1 个月之间的所有记录.

日期是这样存储的 DD-MM-YYYY

以及我尝试过的查询:

<预><代码>SELECT * from tablename WHERE refreshDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)

但这不会返回正确的结果.

解决方案

日期是否存储在实际日期或日期时间字段中?如果它在 char/varchar 字段中,您将无法使用 BETWEEN 语法,因为 mysql 只会将它们视为固定字符串.

I am trying to select all records in a table which have a date between the current date and 1 month ahead.

The date is stored like this DD-MM-YYYY

And the query I have tried:


SELECT * from tablename WHERE renewalDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND  DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)

But this does not return the correct results.

解决方案

Is the date stored in an actual date or datetime field? If it's in a char/varchar field, you won't be able to use the BETWEEN syntax, as mysql will just treat them as fixed strings.

这篇关于mySQL 选择下个月到期的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆