1
我有一個表名empdetail
和列:需要單個SQL查詢
Id Name Gender
1 ABC Male
2 XYZ Female
3 PQR Male
我想改變每個EMP的Gender
從Male
到Female
和Female
到Male
單查詢
結果應該是這樣的:
1 ABC Female
2 XYZ Male
3 PQR Female
我有一個表名empdetail
和列:需要單個SQL查詢
Id Name Gender
1 ABC Male
2 XYZ Female
3 PQR Male
我想改變每個EMP的Gender
從Male
到Female
和Female
到Male
單查詢
結果應該是這樣的:
1 ABC Female
2 XYZ Male
3 PQR Female
你只需要使用CASE聲明
update <table>
set Gender= case when Gender='Male' then 'Female'
when Gender='Female' then 'Male'
end
select id,name,
(case when gender='Male'
then 'Female'
else 'Male'
end) as 'Gender'
from table
-------------------
update table set gender=(case when gender='Male'
then 'Female' else 'Male' end)