whitedragon101 Posted July 24, 2009 Share Posted July 24, 2009 Need to get the lowest of 2 dates. I am trying this syntax but it does not work. I am sure there is some simple change I need to make it work. Any ideas? SELECT MIN('2012-10-09','2011-10-01'); thanks Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/ Share on other sites More sharing options...
Maq Posted July 24, 2009 Share Posted July 24, 2009 Assuming they're 'DATE' types, you can use: ORDER BY date_field ASC LIMIT 2 Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/#findComment-882241 Share on other sites More sharing options...
akitchin Posted July 24, 2009 Share Posted July 24, 2009 are you getting an error? if so, what is it? is that the exact string you're passing to MySQL? if so, you should remove the semicolon. Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/#findComment-882242 Share on other sites More sharing options...
whitedragon101 Posted July 24, 2009 Author Share Posted July 24, 2009 The error is ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '2011-10-01')' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/#findComment-882247 Share on other sites More sharing options...
whitedragon101 Posted July 24, 2009 Author Share Posted July 24, 2009 This is the string I am passing into the MySQL console so need a semicolon to terminate the string. The following is an exact copy of a line I used in the console with DATEDIFF, which does return the difference between the two dates. mysql > SELECT DATEDIFF('2012-10-09','2011-10-01'); works However the query I need now is to get the lowest of the two dates and am again testing it in the console and: mysql > SELECT MIN('2012-10-09','2011-10-01'); returns the error listed above. Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/#findComment-882254 Share on other sites More sharing options...
Maq Posted July 24, 2009 Share Posted July 24, 2009 MIN() is an aggregate function that takes a field, not a value. Try: SELECT date_field FROM table ORDER BY date_field ASC LIMIT 2 Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/#findComment-882258 Share on other sites More sharing options...
whitedragon101 Posted July 24, 2009 Author Share Posted July 24, 2009 What I have is a table structured thus: Job ID | Job Name | StartDate1 | EndDate1 | StartDate2 | EndDate2 | StartDate3 | EndDate3 I have been trying to get the earliest of the start dates (and after that I will need the latest of the end dates). I want to display a table the just has the first two fields then the earliest and latest dates for each row like this: Job ID | Job Name | Earliest Date | Latest Date This is why I am having trouble finding a solution because it is for the ealiest and latest dates for each ROW not COLUMN. I have been trying to do it like this. ($row_job_set is a row of a simple SELECT * query from the table described above ). <table border="1"> <tr> <td>Job Id</td> <td>Job Name</td> <td>Earliest Date</td> <td>Latest Date</td> </tr> <?php do { $startDate1 = $row_job_set['startDate1']; $startDate2 = $row_job_set['startDate2']; $startDate3 = $row_job_set['startDate3']; $lowestDateQuery = "SELECT MIN('$startDate1','$startDate2','$startDate3')"; $lowestDate = mysql_query($lowestDateQuery); $dRow = mysql_fetch_row($lowestDate); $dResult = mysql_result($dRow,0); $totalRows_lowestDate= mysql_num_rows($lowestDate); ?> <tr> <td><?php echo $row_job_set['job_id']; ?></td> <td><?php echo $row_job_set['job_name']; ?></td> <td><?php echo $totalRows_lowestDate ?> </td> <td>Not yet implimented</td> </tr> <?php } while ($row_job_set = mysql_fetch_assoc($job_set)); ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/#findComment-882279 Share on other sites More sharing options...
akitchin Posted July 24, 2009 Share Posted July 24, 2009 ... why would you use MySQL for this instead of PHP? the data's already in PHP. my suggestion would be to select a UNIX_TIMESTAMP()d version of the dates to compare, and then use PHP's min function. Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/#findComment-882281 Share on other sites More sharing options...
whitedragon101 Posted July 24, 2009 Author Share Posted July 24, 2009 Thanks guys Have used the strtotime() to convert the date to a unix timestamp. Then used the php min() function to find the lowest date. The used the gmdate("Y-m-d ", $lowestDate) function to convert the lowest date into a printable date again. Quote Link to comment https://forums.phpfreaks.com/topic/167320-solved-mysql-lowest-of-2-dates/#findComment-882325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.