简体   繁体   中英

MySQL Order By Combining Values in Two Fields

I have a table with two columns, ai1 and ai2. Both are float(6,2) fields, and contain values like below:

ai1      ai2
195.00   193.75
190.60   192.85
189.63   180.00

I would like to be able to sort the data in descending order regardless of the column that contains the value. In other words, like this:

195.00
193.75
192.85
190.60
189.63
180.00

I made a feeble attempt at an IF statement in my Order By which failed. I've also searched here and seen many references to using CASE, but I don't know if that would apply here or how it would work if it does.

I appreciate any guidance. Thanks in advance.

combined them using UNION

SELECT ai1 ai FROM tableName
UNION ALL
SELECT ai2 ai FROM tablename
ORDER BY ai DESC

by the way, specifying ALL will keep duplicate value. If you want to display only unique values remove ALL keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM