Forum Moderators: coopster

Message Too Old, No Replies

MySQL greater than today query problem

         

calebic

3:42 am on Jun 27, 2005 (gmt 0)

10+ Year Member



Hi All,

I'm having a difficult time to get my query to only show records with dates greater than today. Currently it returns all records in the table. My date format is yyyy-mm-dd. The query i am trying to run is:

SELECT * FROM tbl_events WHERE e_Date_Start >= '" & NOW() & "' and status='Active' ORDER BY e_Date_Start ASC

I've tried using the CURDATE() function and have played around with the operator to see what it going on. It seems like it is just not making the date calculation.

Any assistance is greatly appreciated!

Best regards.

dreamcatcher

8:11 am on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



calebic, welcome to Webmaster World. :)

You can do this:

SELECT * FROM tbl_events WHERE e_Date_Start >= '" . date("Y-m-d") . "' and status='Active' ORDER BY e_Date_Start ASC

dc

calebic

3:37 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Hi Dreamcatcher,

Thanks for your reply and your welcome message. I updated my query with your feedback, but am still having difficulties. I tried using the query, exactly as you had it:

SELECT * FROM tbl_events WHERE e_Date_Start >= '" . date("Y-m-d") . "' and status='Active' ORDER BY e_Date_Start ASC

However, the .(periords) threw and expected end of statement error. So I put the ampersands back in and now have:
SELECT * FROM tbl_events WHERE e_Date_Start >= '" & date("Y-m-d") & "' and status='Active' ORDER BY e_Date_Start ASC

This is throwing the error: rong number of arguments or invalid property assignment: 'Date'

Thanks so much for any assistance you can offer.

Best regards

dreamcatcher

5:02 pm on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you using a DATE field type to store your date? The query should be ok.

calebic

5:24 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Hello again,

I am using the date data type in my database. Currently my dates are stored in yyyy-mm-dd format.

I'm stumped too as to why this is not working. If you have any other thoughts, I would love to hear them. Thanks so much for your assistance!

Best regards

calebic

5:25 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Just a thought ... the page I am working on is an asp page. Do you think this could have anything to do with the errors I am experiencing?

Thanks again!

moltar

5:30 pm on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This works for me:

SELECT * 
FROM tbl_events
WHERE 1
AND e_Date_Start > NOW()
AND status = 'Active'
ORDER BY e_Date_Start ASC

calebic

5:53 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Thank you, thank you! This query also worked for me! Can you explain the "Where 1" portion of the query? I don't understand what this portion of the query is doing.

Thank you so much once again.

Best regards.

moltar

5:58 pm on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



WHERE 1 is strictly presentational. It's not really doing anything. 1 is true. So basically it says WHERE TRUE, which is always true. I write it like this so that the AND queries nicely align. It is also very usefull if you are constructing the query via a loop.

I think the problem was with & "' NOW() & '" part. You don't need the quotes in this case. NOW() is escaped by default.

Also you said you want greater than today. But your query read >= which in fact is greater or equal. I am not sure if that played any role.

calebic

6:31 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Awesome ... thanks again! I really appreciate your assistance!

Best regards.

dreamcatcher

9:04 pm on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a thought ... the page I am working on is an asp page

LOL, yes that would be a BIG problem when using PHP code. I figured you were using PHP as this is a PHP forum. Sorry.