本文介绍了订购多列ASC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库布局:

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

我的问题:如何重新排列列,以便最低价格出现在第一列中,第二个出现在第二个ETC中??

My question: How can I re order the columns so the lowest price appear in the first column, the second one in the second ETC..?

我想获得此输出(价格顺序):

I want to get this output (Price order):

ID: 1 - 4.99 - 19.99 - 29.99  
ID: 2 - 14.54 - 45.94 - 87.51  
ID: 3 - 12.58 - 45.87 - 75.54  

谢谢.

推荐答案

我认为最好在应用程序层上通过PHP来做到这一点,但是您可以使用LEAST()GREATEST()来做到这一点:

I think its best to do it via PHP on the application layer , but you can do it with LEAST() and 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

这篇关于订购多列ASC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:37