jonmrich
jonmrich

Reputation: 4333

Update values in column using CASE

I have column of ages and I'm trying to add a column of age ranges based on the value in the age column. I have an age_range column and I was trying this:

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

But I'm getting an error:

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

What am I doing wrong?

Upvotes: 0

Views: 30

Answers (1)

M0rtiis
M0rtiis

Reputation: 3784

you dont need this here:

AS age_range
FROM mytable

try this

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

Upvotes: 3

Related Questions