开发者

SQL Query for an entry where current date is inside start/end dates

Using MySQL

The two fields I need to compare the current date to are: 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.


You should be able to do that, but you have to use BETWEEN:

 WHERE CUR_DATE() BETWEEN `startDate` AND `endDate`

What SQL database are you using?

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.

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.


... WHERE CUR_DATE() BETWEEN `startDate` AND `endDate` ORDER ...


...WHERE `startDate` <= CUR_DATE() and `endDate` >= CUR_DATE()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜