Table
ID DESC
1 Male
2 Male
3 Male
4 Female
5 Female
6 Female
7 Female
我需要使用一个更新语句将男性更新为女性,将女性更新为男性。请就此提出建议。提前致谢。
Table
ID DESC
1 Male
2 Male
3 Male
4 Female
5 Female
6 Female
7 Female
我需要使用一个更新语句将男性更新为女性,将女性更新为男性。请就此提出建议。提前致谢。
尝试这个
UPDATE Table
SET DESC = CASE
WHEN DESC= 'Male' THEN 'Female'
WHEN DESC = 'Female' THEN 'Male'
ELSE DESC
END
输出如下
ID DESC
1 Female
2 Female
3 Female
4 Male
5 Male
6 Male
7 Male
是的,您可以使用case语句来做到这一点
UPDATE TABLE
SET DESC = CASE WHEN DESC = 'MALE' THEN 'Female' WHEN DESC = 'Female' THEN 'MALE' ELSE DESC END
对于 SQL 服务器
update tbl
SET DESC = CASE
WHEN DESC= 'Male' THEN 'Female'
WHEN DESC = 'Female' THEN 'Male'
ELSE DESC
END