AusTex
AusTex

Reputation: 205

compare dates in mysql where the day, month and year are stored separately

I would like to compare several entries, for which I have the day, month and year saved in separate variables. When I try the following, I do not get the right answer:

    $rescount=mysql_query('SELECT COUNT(*) FROM  `sdcheckings` WHERE  `dchecked` = 1 AND  `email` ="'.$_SESSION['email'].'" AND  `cday` >'.date('j').' AND  `cmonth` >='.date('n').' AND  `cyear` >='.date('Y'));

The reason is that this would ignore and entry that is in the following month, but that has a smaller day number. What would be the easiest way to fix this?

Upvotes: 3

Views: 99

Answers (2)

AusTex
AusTex

Reputation: 205

Here is what works now, thanks!

    $rescount=mysql_query('SELECT COUNT(*) FROM  `sdcheckings` WHERE  `dchecked` = 1 AND  `email` ="'.$_SESSION['email'].'" AND  DATE(CONCAT_WS("-",`cyear`,`cmonth`,`cday`)) > CURDATE()');

Upvotes: 0

Bart
Bart

Reputation: 1268

I think that you can use CONCAT_WS to create date in query

DATE(CONCAT_WS('-',cyear,cmonth,cdate)) AS date

Then you got date in yyyy-mm-dd format to compare

Upvotes: 2

Related Questions