Saeid Khaleghi Saeid Khaleghi - 6 months ago 11
MySQL Question

SQL Ordering by field value

I Have following table.

enter image description here

Simply i want to make order by

meta_key
where value is
LoginTS
. its a bit distracting.

ORDER BY meta_value( where meta_key is LoginTS ) DESC
.

Im sorry if its not clear enough..


Expected Result :
enter image description here

Answer
SELECT * 
FROM   mytable 
WHERE `meta_key`= 'LoginTS' 
ORDER  BY `meta_value` DESC 

SQL FIDDLE DEMO 1

If you want to get back all the Table but to ORDER BY specific column try this:

SELECT
 *
FROM
  myTable
  ORDER BY CASE WHEN `meta_key`='LoginTS' THEN 0 ELSE 1 END

SQL FIDDLE DEMO 2