我正在迁移数据库,我需要将整数列更改为字符串格式的相关类别。如何使用ifelse或switch / case / break来检查每个值并动态更改?
SELECT *, Category FROM Members
Category =
Switch(category)
if (value == 1 then change to "Category 1", value == 2 then change to "Category 2 and so on.)
答案 0 :(得分:1)
SELECT *, case when category = 1 then 'Category 1'
when category = 2 then 'Category 2'
else 'Category unknown'
end as category
FROM Members
答案 1 :(得分:0)
MySQL的CASE子句可以做到这一点。
UPDATE `Members`
Set `Category` = CASE `Value`
WHEN '1' THEN 'Category 1'
WHEN '2' THEN 'Category 2'
END
WHERE `Value` IN ('1','2');
这意味着任何带有Value = '1'
的和所有行现在也会有Category = 'Category 1'
。您可以根据需要使此查询更具体。