Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2012, 07:43 PM   PM User | #1
chidambaram1987
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
chidambaram1987 is an unknown quantity at this point
Select date time for the different status of the field

I am looking for help to select the datatime of the particular status .. 1) Min date time for staus 1 and 2) Max date time for status 2

+---------------------+--------+
| online | status |
+---------------------+--------+
| 2012-10-09 21:30:12 | 1 |
| 2012-10-09 22:30:24 | 0 |
| 2012-10-09 23:30:44 | 1 |
| 2012-10-09 23:30:47 | 0 |
+---------------------+--------+

Min time of status 1 and max time of status 0 is required please

Last edited by chidambaram1987; 10-09-2012 at 07:45 PM..
chidambaram1987 is offline   Reply With Quote
Old 10-09-2012, 08:46 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
SELECT MIN( IF(status=1,online,'2099-12-31') ) AS minStatus1,
       MAX( IF(status=2,online,'1900-1-1') ) AS maxStatus2
FROM table
Naturally, the "dummy" values there (2099-12-31 and 1900-1-1) can be anything you choose that are outside the possible range of value in your actual data.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-09-2012, 08:50 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You could also force the code to get a NULL if there isn't any match on status=1 or status=2, if you need that:
Code:
SELECT IF( x.s1 = '2099-12-31', NULL, x.s1 ) AS minStatus1,
       IF( x.s2 = '1900-1-1', NULL, x.s2 ) AS maxStatus2
FROM (
    SELECT MIN( IF(status=1,online,'2099-12-31') ) AS s1,
           MAX( IF(status=2,online,'1900-1-1') ) AS s2
    FROM table
    ) AS x
But if you know there is at least one good value for status=1 and one good value for status=2 then don't bother with that.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:06 PM.


Advertisement
Log in to turn off these ads.