select - Show name of columns in mysql -
i have 3 date values in table, namely activationdate, registrationdate, , creationdate , have select show name of column least value.
i used
select least(activationdate, registrationdate, creationdate) table name
and returns smallest value. however, want know of these 3 smallest value. so, expected outcome name of column has least date can activation date, registration date or creation date.
for solution, kindly state result if 3 of them have similar values? return or one?
thanks
you need test least
value against each column, in conditional statement.
assuming following table structure
create table `test` ( `activationdate` datetime default null, `registrationdate` datetime default null, `creationdate` datetime default null )
with these values
mysql> select * test; +---------------------+---------------------+---------------------+ | activationdate | registrationdate | creationdate | +---------------------+---------------------+---------------------+ | 2015-01-01 00:00:00 | 2015-01-02 00:00:00 | 2015-01-03 00:00:00 | | 2015-01-30 00:00:00 | 2015-01-15 00:00:00 | 2015-01-12 00:00:00 | +---------------------+---------------------+---------------------+
you can use following query extract smallest of values, , column name:
select least(activationdate, registrationdate, creationdate) value, case when least(activationdate, registrationdate, creationdate) = activationdate 'activationdate' when least(activationdate, registrationdate, creationdate) = registrationdate 'registrationdate' when least(activationdate, registrationdate, creationdate) = creationdate 'creationdate' end field test;
and give these results:
+---------------------+----------------+ | value | field | +---------------------+----------------+ | 2015-01-01 00:00:00 | activationdate | | 2015-01-12 00:00:00 | creationdate | +---------------------+----------------+
Comments
Post a Comment