★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ekavxfwr-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
SQL架構git
1 create table if not exists salary(id int, name varchar(100), sex char(1), salary int) 2 Truncate table salary 3 insert into salary (id, name, sex, salary) values ('1', 'A', 'm', '2500') 4 insert into salary (id, name, sex, salary) values ('2', 'B', 'f', '1500') 5 insert into salary (id, name, sex, salary) values ('3', 'C', 'm', '5500') 6 insert into salary (id, name, sex, salary) values ('4', 'D', 'f', '500')
Given a table salary
, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table. github
For example: sql
| id | name | sex | salary | |----|------|-----|--------| | 1 | A | m | 2500 | | 2 | B | f | 1500 | | 3 | C | m | 5500 | | 4 | D | f | 500 |
After running your query, the above salary table should have the following rows:微信
| id | name | sex | salary | |----|------|-----|--------| | 1 | A | f | 2500 | | 2 | B | m | 1500 | | 3 | C | f | 5500 | | 4 | D | m | 500 |
給定一個 salary
表,以下所示,有m=男性 和 f=女性的值 。交換全部的 f 和 m 值(例如,將全部 f 值更改成 m,反之亦然)。要求使用一個更新查詢,而且沒有中間臨時表。架構
例如:spa
| id | name | sex | salary | |----|------|-----|--------| | 1 | A | m | 2500 | | 2 | B | f | 1500 | | 3 | C | m | 5500 | | 4 | D | f | 500 |
運行你所編寫的查詢語句以後,將會獲得如下表:code
| id | name | sex | salary | |----|------|-----|--------| | 1 | A | f | 2500 | | 2 | B | m | 1500 | | 3 | C | f | 5500 | | 4 | D | m | 500 |
129ms
1 # Write your MySQL query statement below 2 select id, name, if(sex = 'm','f','m') sex,salary from salary;
281mshtm
1 UPDATE salary 2 SET sex = CASE 3 WHEN sex = 'f' THEN 'm' 4 ELSE 'f' 5 END
282msblog
1 # Write your MySQL query statement below 2 update salary set sex= if(sex='m','f','m');
283ms
1 # Write your MySQL query statement below 2 UPDATE salary 3 SET 4 sex = CASE sex 5 WHEN 'm' THEN 'f' 6 ELSE 'm' 7 END;
284ms
1 update salary set sex=case sex when 'm' then 'f' else 'm' end;
287ms
1 # Write your MySQL query statement below 2 update salary set sex= CHAR(ASCII('f') + ASCII('m') - ASCII(sex));