user2680182
user2680182

Reputation: 55

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.

Upvotes: 1

Views: 340

Answers (1)

John Woo
John Woo

Reputation: 263693

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.

Upvotes: 3

Related Questions