我正在尝试向表中添加一个列,该表需要从表中的现有列获取varchar值,并将其转换为if / then格式的int。
示例:
if size = d then size_int = 1
else if size = f then size_int = 2
else if size = t then size_int = 3
else if size = s then size_int = 4
else if size = m then size_int = 5
else if size = l then size_int = 6
else if size = h then size_int = 7
else if size = g then size_int = 8
else if size = c then size_int = 9
如果有一种更简单的方法可以通过首先添加列然后对其进行更改来实现此目的。
答案 0 :(得分:3)
试试这个:
ALTER TABLE <YOUR_TABLE> ADD size_int INT;
UPDATE <YOUR_TABLE> SET size_int =
CASE size
WHEN 'd' THEN 1
WHEN 'f' THEN 2
WHEN 't' THEN 3
WHEN 's' THEN 4
WHEN 'm' THEN 5
WHEN 'l' THEN 6
WHEN 'h' THEN 7
WHEN 'g' THEN 8
WHEN 'c' THEN 9
ELSE NULL
END