我的数据库布局:
ID Description Shop 1 Shop 2 Shop 3
1 | Article Description | 19.99 | 29.99 | 4.99
2 | Article Description | 45.94 | 14.54 | 87.51
3 | Article Description | 75.54 | 12.58 | 45.87
我的问题:如何重新订购列,以便第一列显示最低价格,第二列显示第二列..?
我想得到这个输出(价格订单):
ID: 1 - 4.99 - 19.99 - 29.99
ID: 2 - 14.54 - 45.94 - 87.51
ID: 3 - 12.58 - 45.87 - 75.54
感谢。
答案 0 :(得分:2)
我认为最好通过PHP在应用程序层上执行此操作,但您可以使用LEAST()
和GREATEST()
执行此操作:
SELECT t.id,t.description,
LEAST(t.shop1,t.shop2,t.shop3) as lowest,
(t.shop1+t.shop2+t.shop3) - (LEAST(t.shop1,t.shop2,t.shop3) + GREATEST(t.shop1,t.shop2,t.shop3)) as middle_one
GREATEST(t.shop1,t.shop2,t.shop3) as biggest
FROM YourTable t