查询以根据MYSQL中其他字段的结果添加新列

时间:2014-08-21 21:26:23

标签: mysql sql

目前我有一张桌子

id  amount_spend
1   10
2   20
3   30
4   40
5   50

我想添加名为category的第三列,它应显示:

  • 高(如果amount_spend> 30),
  • medium(amount_spend> 20和< 30)和
  • low(amount_spend> 20)。

请让我知道如何在选择声明中添加它。

1 个答案:

答案 0 :(得分:3)

select id, 
       amount_spend, 
       case
          when amount_spend > 30 then 'high'
          when amount_spend > 20 and amount_spend <= 30 then 'medium'
          else 'low'
       as category
from the_table;