使用CASE更新列中的值

时间:2015-09-19 01:18:33

标签: mysql

我有一些年龄段,我试图根据年龄列中的值添加一列年龄段。我有一个age_range列,我正在尝试这个:

UPDATE mytable
SET age_range = 
CASE WHEN age >=1 and age <= 24 then '18-24'
ELSE '' 
END AS age_range
FROM mytable

但是我收到了错误:

check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS age_range
FROM mytable' at line 5

我做错了什么?

1 个答案:

答案 0 :(得分:3)

你不需要这个:

AS age_range
FROM mytable

试试这个

UPDATE mytable
SET age_range = 
CASE WHEN age >=1 and age <= 24 then '18-24'
ELSE '' 
END