显示2列之间哪个值更高

时间:2013-01-16 02:58:11

标签: php mysql sql select

我有一张这样的表

╔═══════╦════════╗
║  VOL  ║ WEIGHT ║
╠═══════╬════════╣
║ 20.00 ║ 222.00 ║
╚═══════╩════════╝

然后我想显示2列之间哪个值更高 这就像......

╔═══════╦════════╦═════════╗
║  VOL  ║ WEIGHT ║ TONNAGE ║
╠═══════╬════════╬═════════╣
║ 20.00 ║ 222.00 ║ 222.00  ║
╚═══════╩════════╩═════════╝

2 个答案:

答案 0 :(得分:5)

使用GREATEST

SELECT vol, weight, GREATEST(vol, weight) Tonnage
FROM tableName

SOURCE

答案 1 :(得分:2)

我同意之前关于GREATEST的帖子,但是对于更通用的语法,CASE效果也很好(应该适用于大多数RDBMS):

SELECT vol, 
  weight, 
  case 
     when vol > weight 
     then vol 
     else weight end as Tonnage
FROM tableName

SQL Fiddle