2012-10-11 222 views
1

我有一個表名empdetail和列:需要單個SQL查詢

Id Name Gender 
    1 ABC Male 
    2 XYZ Female 
    3 PQR Male 

我想改變每個EMP的GenderMaleFemaleFemaleMale單查詢

結果應該是這樣的:

1 ABC Female 
2 XYZ Male 
3 PQR Female 

回答

4

你只需要使用CASE聲明

update <table> 
set Gender= case when Gender='Male' then 'Female' 
       when Gender='Female' then 'Male' 
      end 


SQL fiddle demo

0
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)