Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find lowest date (custom) in mysql

Tags:

sql

php

mysql

I have no idea how can I find the lowest string in date-type

I will just find lowest month(by years in a table)

TableName

Id  | M  | D  |   Y   |
=======================
1  | 01  | 22  | 2012 |
2  | 11  | 29  | 2012 |
3  | 12  | 30  | 2013 |
4  | 01  | 30  | 2011 | <--- this !
5  | 12  | 14  | 2012 |

PHP:

$sql = "SELECT * FROM TableName WHERE M=?? AND Y=??";
$selected = mysql_query($sql);

so $selected should give me a result like "4/01/30/2011" (Id,M,D,Y)

Any?

like image 566
l2aelba Avatar asked Dec 03 '12 11:12

l2aelba


2 Answers

SELECT min(concat(Y,M,D)) FROM TableName

Edit: This just looks nice and clean but it is kind of very bad answer, so please use this answer

like image 155
bugwheels94 Avatar answered Sep 21 '22 21:09

bugwheels94


Just use the ORDER BY clauses:

SELECT * FROM TableName
ORDER BY Y ASC, M ASC, D ASC

More info here : http://www.tizag.com/mysqlTutorial/mysqlorderby.php

like image 28
koopajah Avatar answered Sep 21 '22 21:09

koopajah