Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort columns in MYSQL

Tags:

sql

sorting

mysql

I have three fields in each row and I want to concatenate them, sorted ascending:

select * concat(field1, field2, field3) AS result
from tbl 

My data is:

field1, field2, field3
   7       2       9
   4       7       8
   6       3       2 

The expected result is:

field1, field2, field3   result
  7       2       9       279
  4       7       8       478
  6       3       2       236

How can I do this?

like image 311
mohammad zahedi Avatar asked Sep 01 '25 22:09

mohammad zahedi


1 Answers

Use LEAST ,GREATEST function technically

   SELECT field1, field2, field3,
   concat(LEAST(field1,field2,field3), 
   (field1 + field2 + field3) - (GREATEST(field1,field2,field3)+
   LEAST(field1,field2,field3)),
   GREATEST(field1,field2,field3)
   ) 
   from tbl
like image 156
Zaynul Abadin Tuhin Avatar answered Sep 03 '25 17:09

Zaynul Abadin Tuhin