176. 第二高的薪水
ifnull(a,b)函数解释:如果value1不是空,结果返回a;如果value1是空,结果返回b
select (select DISTINCT salary as SecondHighestSalary from employee order by salary desc limit 1,1) as SecondHighestSalary;
如果子查询啥也没查到,相当于select null as SecondHighestSalary;
177. 第N高的薪水
由于无法直接用limit N-1,所以需先在函数开头处理N为N=N-1。 注:这里不能直接用limit N-1是因为limit和offset字段后面只接受正整数(意味着0、负数、小数都不行)或者单一变量(意味着不能用表达式)
BEGIN
SET N := N-1;
RETURN (
# Write your MySQL query statement below.
select distinct salary from employee order by salary desc limit N,1
);
END
182. 查找重复的电子邮箱
where中不能使用聚合函数,要想使用聚合函数,使用group by,在约束条件having中使用
select email from person group by email having count(email) >1;
584. 寻找用户推荐人
MySQL 使用三值逻辑 —— TRUE, FALSE 和 UNKNOWN。任何与 NULL 值进行的比较都会与第三种值 UNKNOWN 做比较。这个“任何值”包括 NULL 本身!这就是为什么 MySQL 提供 IS NULL 和 IS NOT NULL 两种操作来对 NULL 特殊判断。
select name from customer where referee_id is null or referee_id <> 2
620. 有趣的电影
MySQL 中判断奇数的 6 种方法:
mod(x, 2) = 1
,如果余数是 1 就是奇数。power(-1, x) = -1
, 如果结果是 -1 就是奇数x % 2 = 1
,如果余数是 1 就是奇数。x & 1 = 1
,如果是 1 就是奇数x regexp '[1, 3, 5, 7, 9]$' = 1
如果为 1 就是奇数x>>1<<1 != x
如果右移一位在左移一位不等于原值,就是奇数
select id,movie,description,rating from cinema where description != 'boring' and id % 2 != 0 order by rating desc