If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Comparing Time in conditional statement

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-09, 16:10
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
Comparing Time in conditional statement

hi, I'm looking to include a condition which compares the current time of day during our MYSQL query. However, I'm having some trouble.

Code:
$query = 'SELECT * FROM jos_keyword_keys WHERE (state = 0 AND (((DATEDIFF(CURDATE(), StartDate) >= 0 AND DATEDIFF(CURDATE(), EndDate) <= 0) OR (StartDate = 0000-00-00 AND EndDate = 0000-00-00)) AND (StartTime = 00:00:00 AND EndTime = 00:00:00)))';
	
	
$query = mysql_query($query);
before trying to implement the TIMEDIFF() method I decided to include the default StartTime and EndTime values, however even these don't seem to be working correctly. Is it the :?
Reply With Quote
  #2 (permalink)  
Old 06-30-09, 00:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
why do you have separate date and time columns?

why do you allow zero values?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-30-09, 11:09
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
because I require that each attribute be as atomic as possible. The application I've developed should only function during certain times/dates. The 0(state) variable indicates whether this entry should be considered trash or usable.

Why are you ask?
Reply With Quote
  #4 (permalink)  
Old 06-30-09, 11:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Quote:
Originally Posted by Schweppesale
Why are you ask?
because a single datetime value is much easier to work with than a combination of date and time values

and what's with the zero values? why aren't you using NULLs?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-30-09, 11:22
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
so what you're telling me is that it would be easier to write a conditional query which compares both time and date when the values are stored in a single attribute? Kind of like using Name instead of using First Name and Last Name
Reply With Quote
  #6 (permalink)  
Old 06-30-09, 11:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Quote:
Originally Posted by Schweppesale
so what you're telling me is that it would be easier to write a conditional query which compares both time and date when the values are stored in a single attribute? Kind of like using Name instead of using First Name and Last Name
you actually don't need me to tell you, you can test it yourself

let's say you wanted all rows between '2009-06-30 09:37' and '2009-07-01 15:55'

example with one column --

WHERE datetimecolumn BETWEEN '2009-06-30 09:37' AND '2009-07-01 15:55'

now it's your turn, show us how to do the same thing when there two columns for date and time
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 06-30-09, 11:42
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
ok, now filter all results by a certain time. Frankly, I don't care how simple the condition statement is. The one that I have now is working fine.

Next time I need you to critic my database I'll post a UML diagram. All I asked what how to compare two Time columns.
Reply With Quote
  #8 (permalink)  
Old 06-30-09, 12:17
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 8,768
Quote:
Originally Posted by Schweppesale
...Next time I need you to critic my database I'll post a UML diagram. All I asked what how to compare two Time columns.
might be a better idea to post your ERD so that potential issues can be caught before they become nagging painful problems

like all phases of design getting it right first time is rare, but y'don't try and build something on a dodgy foundation. if the foundations are dicky to start with then that system is going to be a bastard to maintain over time, and thats where the true cost of developer time gets expensive, picking up the pieces working out how the developer does thiungs and then implementing the changes.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #9 (permalink)  
Old 06-30-09, 12:57
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
alright, I understand what you're saying. My knowledge of MYSQL is shaky at best though. How would I go about filtering my query result should I decide to return only a entries where the time within my TIMEDATE column matches a certain value.

Last edited by Schweppesale; 06-30-09 at 13:00.
Reply With Quote
  #10 (permalink)  
Old 06-30-09, 13:17
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 8,768
post #6 seems to cover it
ie
where adatetimecolumn BETWEEN 'adatetimevalue' AND 'anotherdatetimevalue'
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #11 (permalink)  
Old 06-30-09, 14:13
Schweppesale Schweppesale is offline
Registered User
 
Join Date: Jun 2009
Posts: 33
correct. My question is how would I filter out a specific time though, rather than both the date and time.

For instance, if I want to pull the earliest time then compare it to midnight to see how many entries are within the database.

Normally I'd just take the two times and use a WHERE (Time <= 12:00:00)

with DateTime both of the Date and Time attributes would be combined; that's really the entire reason why I decided to keep both of them seperate.
Reply With Quote
  #12 (permalink)  
Old 06-30-09, 14:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Quote:
Originally Posted by Schweppesale
that's really the entire reason why I decided to keep both of them seperate.
you should have said this in post #3
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 06-30-09, 14:58
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 8,768
you could always try to EXTRACT the time from the datetime value
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On