tmartin314
tmartin314

Reputation: 4171

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.

Upvotes: 0

Views: 1028

Answers (3)

Larry Lustig
Larry Lustig

Reputation: 50970

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.

Upvotes: 2

Joe Stefanelli
Joe Stefanelli

Reputation: 135739

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

Upvotes: 1

Gerben
Gerben

Reputation: 16825

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

Upvotes: 1

Related Questions