我正在迁移数据库,我需要将整数列更改为字符串格式的关联类别。如何检查每个值并使用 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.)
SELECT *, case when category = 1 then 'Category 1'
when category = 2 then 'Category 2'
else 'Category unknown'
end as category
FROM Members
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'
. 您可以根据需要使此查询更加具体。