1

假设我有一张桌子,里面有一堆产品,有些是特价的,有些是二手的。

| Desc       |  Sale price | Special price | Used price
--------------------------------------------------------
| Product 1  |       20.00 |         15.00 |       0.00
| Product 2  |       38.00 |          0.00 |      22.00
| Product 3  |       21.00 |          0.00 |       0.00
| Product 4  |       16.00 |         12.00 |       8.00
| Product 5  |        0.00 |          0.00 |      25.00

我正在尝试找出一种方法来对产品(在同一查询中)从三列中的最低值到最高值进行排序,同时忽略所有的 0.00(例如:已使用 0.00 表示没有可用于该特定产品)..

结果是这样的:

| Desc       |  Sale price | Special price | Used price
--------------------------------------------------------
| Product 4  |       16.00 |         12.00 |      *8.00   8.00 is the lowest
| Product 1  |       20.00 |        *15.00 |       0.00   15.00 is the lowest, 1x 0.00 is ignored
| Product 3  |      *21.00 |          0.00 |       0.00   21.00 is the lowest, 2x 0.00 are ignored
| Product 2  |       38.00 |          0.00 |     *22.00   22.00 is the lowest, 1x 0.00 is ignored
| Product 5  |        0.00 |          0.00 |     *25.00   25.00 is the lowest, 2x 0.00 are ignored

任何帮助将不胜感激。谢谢

4

1 回答 1

0

LEAST()这是一个使用和的技巧GREATEST()

ORDER BY LEAST(IF(SalePrice = 0, GREATEST(SalePrice, SpecialPrice, UsedPrice) + 1, SalePrice), 
               IF(SpecialPrice = 0, GREATEST(SalePrice, SpecialPrice, UsedPrice) + 1, SpecialPrice), 
               IF(UsedPrice = 0, GREATEST(SalePrice, SpecialPrice, UsedPrice) + 1, UsedPrice)) ASC
于 2013-08-06T14:48:17.777 回答