[英]SQL Query for an entry where current date is inside start/end dates
Using MySQL 使用MySQL
The two fields I need to compare the current date to are: startDate, endDate 我需要比较当前日期的两个字段是:startDate,endDate
I need a query something like this: 我需要这样的查询:
SELECT * FROM `{$this->_sPrefix}`
WHERE `profileID` = '{$iProfileId}'
AND CUR_DATE()
BETWEEN `startDate` AND `endDate`
ORDER BY `added` DESC";
This is my new query but I don't know how to do both checks: profileID and the date together. 这是我的新查询,但我不知道如何同时执行两个检查:profileID和日期。
You should be able to do that, but you have to use BETWEEN: 您应该能够做到这一点,但是必须使用BETWEEN:
WHERE CUR_DATE() BETWEEN `startDate` AND `endDate`
What SQL database are you using? 您正在使用什么SQL数据库?
Edit (in response to edited question, and please don't keep changing the question!): 编辑(针对已编辑的问题,请不要继续更改问题!):
Your query looks correct, assuming that profileID, startDate, and endDate are all columns in the same table. 假设profileID,startDate和endDate是同一表中的所有列,则查询看起来正确。
Also, if this is a web-facing application, please consider avoiding this kind of dynamic SQL generation as it makes your application open to SQL injection attacks. 另外,如果这是一个面向Web的应用程序,请考虑避免这种动态SQL生成,因为它会使您的应用程序容易受到SQL注入攻击。
... WHERE CUR_DATE() BETWEEN `startDate` AND `endDate` ORDER ...
...WHERE `startDate` <= CUR_DATE() and `endDate` >= CUR_DATE()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.