Yolo-929 / DB_Leetcode

不定期更新数据库算法题目,帮助大家巩固数据库知识
1 stars 1 forks source link

5.26 #1

Open Yolo-929 opened 5 years ago

Yolo-929 commented 5 years ago

Solution

开胃菜当然most easiest啦~

解法1

-- where以及or
select name,population,area from world
where population > 25000000 or area > 3000000;

解法2(较快,但这个数据量太小看不出)

-- where以及union
select name,population,area from world
where population > 25000000 
UNION
select name,population,area from world
where area >3000000;
Yolo-929 commented 5 years ago

Solution

解法1

--利用ASCII码
update salary
-- 同一个数异或两次为本身 a^b^b=a
SET sex=char(ASCII(sex)^ASCII('m')^ASCII('f'));
-- 或者这样
--SET sex = CHAR(ASCII('f') + ASCII('m') - ASCII(`sex`);

解法2

--利用CASE
UPDATE salary
SET
    sex = CASE sex
        WHEN 'm' THEN 'f'
        ELSE 'm'
    END;

解法3

--利用IF
UPDATE salary 
SET sex = IF(sex = 'm', 'f', 'm')

解法4

--利用REPLACE()
UPDATE salary 
SET sex = REPLACE ('fm', sex, '');