-1

tabl_priceList:

+--------+------+------+------+-------
| ProductID| price1 | price2 | price3 |
+--------+--------+------+------+------
|   10001 |   30    |  56    |   23   |
|   10002 |   650   | 560    |  675   |
|   10003 |   57    |  50    |  60    |
|   10004 |  300    | 305    |  3009  |
+--------+------+------+------+-------

I need to find out minimum column value for each row.

its look like.

-----+------+------+---
| ProductID| smallest | 
+--------+--------+-----
|   10001 |    23     | 
|   10002 |    560    | 
|   10003 |    50     | 
|   10004 |    300    | 
+--------+------+------

Please help me to solve the problem

4

2 回答 2

7

只需使用LEAST()

SELECT ProductID, LEAST(price1, price2, price3) smallest
FROM   tabl_priceList

输出

╔═══════════╦══════════╗
║ PRODUCTID ║ SMALLEST ║
╠═══════════╬══════════╣
║     10001 ║       23 ║
║     10002 ║      560 ║
║     10003 ║       50 ║
║     10004 ║      300 ║
╚═══════════╩══════════╝
于 2013-05-17T05:26:19.143 回答
0

或者,您也可以使用CASE相同的语句:

SELECT
ProductID
, CASE WHEN price1 <= price2 and price1 <= price3 THEN Price1
       WHEN price2 <= price1 and price2 <= price3 THEN Price2
       ELSE price3 END AS smallest
FROM tabl_priceList;

输出:

╔═══════════╦══════════╗
║ PRODUCTID ║ SMALLEST ║
╠═══════════╬══════════╣
║     10001 ║       23 ║
║     10002 ║      560 ║
║     10003 ║       50 ║
║     10004 ║      300 ║
╚═══════════╩══════════╝

看到这个 SQLFiddle

于 2013-05-17T05:27:58.650 回答