LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2009, 05:12 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
more or less datetime in mysql


Hello all,

I have a table that I am trying to select records from where the record is greater or equal to one date and equal or less than another date, I have found answers on BETWEEN two dates but not on less/more and equal to:

i.e:
what I am trying to acheive:
Code:
select user_id, start_dstamp, end_dstamp 
from users u 
where DATE(u.start_dstamp)<=DATE('2009-9-30') 
AND DATE(u.end_dstamp)>=DATE('2009-9-30') 
ORDER BY start_dstamp
;
however i always get no results found?


the data in the table:
Code:
select user_id, start_dstamp, end_dstamp from users;

user_id, start_dstamp,          end_dstamp
1  	 2009-08-11 00:00:00  	2009-08-11 23:59:00
4 	 2009-08-24 00:00:00 	2009-08-24 23:59:00
10 	 2009-08-28 00:00:00 	2009-08-28 23:59:00
14 	 2009-09-10 12:00:00 	2009-09-10 00:00:00
16 	 2009-09-28 00:00:00 	2009-09-30 23:59:00

start_dstamp and end_dstamp are both type datetime.


could someone show me what the correct syntax for this would be?

cheers in advance, trscookie.
 
Old 09-11-2009, 10:12 AM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

I created a table like yours and checked the result, it works for me:
Code:
mysql> select user_id, start_dstamp, end_dstamp
    -> from users u
    -> where DATE(u.start_dstamp)<=DATE('2009-9-30')
    -> AND DATE(u.end_dstamp)>=DATE('2009-9-30')
    -> ORDER BY start_dstamp;
+---------+---------------------+---------------------+
| user_id | start_dstamp        | end_dstamp          |
+---------+---------------------+---------------------+
|      16 | 2009-09-28 00:00:00 | 2009-09-30 23:59:00 |
+---------+---------------------+---------------------+
1 row in set (0.02 sec)
That's the result I expected.

You can get the same result using BETWEEN:
Code:
mysql> select user_id, start_dstamp, end_dstamp from users where DATE('2009-09-30') between DATE(start_dstamp) AND DATE(end_dstamp) ORDER BY start_dstamp;
+---------+---------------------+---------------------+
| user_id | start_dstamp        | end_dstamp          |
+---------+---------------------+---------------------+
|      16 | 2009-09-28 00:00:00 | 2009-09-30 23:59:00 |
+---------+---------------------+---------------------+
1 row in set (0.00 sec)
Jan
 
Old 09-11-2009, 10:31 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
hummm, i remember seeing something similar when i tried to update via user_id. Ill drop the table and re-create it just in case its got its-self in a bit of a pickle, not quite sure what else to do :S, thanks anyway.
 
Old 09-11-2009, 08:59 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
2009-09-30 translates to 2009-09-30 00:00:00 hence you cannot find any record which is both > 2009-09-30 and < 2009-09-30.

You could improve your query by search for > 2009-09-30 00:00 and < 2009-09-30 23:59

jlinkels
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl DateTime abdul_zu Linux - General 1 01-14-2006 02:55 AM
MySQL datetime jabfinger Programming 2 07-25-2005 12:27 PM
php :: datetime gmarais Programming 3 03-06-2004 04:33 PM
Mysql/PHP query problem with datetime field. Pcghost Programming 2 11-11-2003 12:24 PM
html form current datetime meluser Programming 27 04-16-2003 06:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration