MySQL Forums
Forum List  »  Newbie

MySQL query
Posted by: Frederick Abeloos
Date: July 21, 2010 06:42PM

Hello everyone,

I hope someone could help me with the following issue.

I'm creating a planning site and have the following problem.

I do have a table with records containing different fields, where one field is a datetime.

Now I have a PHP script where I need to get the datetime of the record where this datetime is "the last datetime just before a certain date.

For instance, if I set the date to 2010-07-21, then I need the datetime out of the table mentioned above, that is the last one before 2010-07-21.

Eg. if the table containing the data has following records

record 1: field 1, field 2, field 3, ..., field x (containing: 2010-07-10)
record 2: field 1, field 2, field 3, ..., field x (containing: 2010-07-16)
record 3: field 1, field 2, field 3, ..., field x (containing: 2010-07-20)
record 4: field 1, field 2, field 3, ..., field x (containing: 2010-07-22)
record 5: field 1, field 2, field 3, ..., field x (containing: 2010-07-26)

And in the PHP script I put the date to 2010-07-21 ($date="2010-07-21"), then I need the return 2010-07-20 eg $record_date="2010-07-20".

How can I query this? I presume it should be something with "select * from table"... and in the while section some kind of loop where I put $record_date to the record field if it is higher then the previous one and lower then the defined $date variable (2010-07-21)... or is there a better solution...

Could someone help me with the loop or the better solution?

My second question is, how can I check if there is a record containing a field with a certain value?

If there is a record containing 2010-07-21, then I need another script to be executed, if not, I need the date mentioned above (the last datetime just before 2010-07-21 of all records...)

Could someone please help me? I would very much appreciate it!



Options: ReplyQuote

Written By
MySQL query
July 21, 2010 06:42PM
July 21, 2010 09:08PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.