{"id":2416,"date":"2018-04-03T08:17:59","date_gmt":"2018-04-03T15:17:59","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2416"},"modified":"2018-04-03T08:18:21","modified_gmt":"2018-04-03T15:18:21","slug":"leetcode-627-swap-salary","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sql\/leetcode-627-swap-salary\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 627. Swap Salary"},"content":{"rendered":"<h1>Problem<\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/swap-salary\/description\/\">https:\/\/leetcode.com\/problems\/swap-salary\/description\/<\/a><\/p>\n<div class=\"question-description\">\n<div>\n<p>Given a table\u00a0<code>salary<\/code>, 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.For example:<\/p>\n<pre class=\"crayon:false\">| id | name | sex | salary |\r\n|----|------|-----|--------|\r\n| 1  | A    | m   | 2500   |\r\n| 2  | B    | f   | 1500   |\r\n| 3  | C    | m   | 5500   |\r\n| 4  | D    | f   | 500    |\r\n<\/pre>\n<p>After running your query, the above salary table should have the following rows:<\/p>\n<pre class=\"crayon:false\">| id | name | sex | salary |\r\n|----|------|-----|--------|\r\n| 1  | A    | f   | 2500   |\r\n| 2  | B    | m   | 1500   |\r\n| 3  | C    | f   | 5500   |\r\n| 4  | D    | m   | 500    |\r\n<\/pre>\n<\/div>\n<\/div>\n<h1><strong>Solution: XOR<\/strong><\/h1>\n<pre class=\"lang:mysql decode:true \"># Author: Huahua\r\n# Running time: 309 ms\r\nupdate salary set sex = char(ascii('f') ^ ascii('m') ^ ascii(sex))<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem https:\/\/leetcode.com\/problems\/swap-salary\/description\/ Given a table\u00a0salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[238],"tags":[242,290,63],"class_list":["post-2416","post","type-post","status-publish","format-standard","hentry","category-sql","tag-sql","tag-swap","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2416"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2416\/revisions"}],"predecessor-version":[{"id":2418,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2416\/revisions\/2418"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}