网上做题随笔--MySql

网上写写题 提高下自己的能力。

Mysql平时写的是真的很少,所以训练一下下。

1.查找重复的电子邮箱

https://leetcode-cn.com/problems/duplicate-emails/

我的解法:

select distinct(t1.Email) from Person t1,Person t2 where t1.Id != t2.Id and t1.Email = t2.Email;

官方题解:

select email from Person group by email having count(email) > 1;

学习了下having的用法。(在此之前没听过。。)

where 是检索前的筛选,而having是检索后的组再筛选。

扫描二维码关注公众号,回复: 6531541 查看本文章

WHERE 子句作用于表和视图,HAVING 子句作用于组。

2.大的国家

https://leetcode-cn.com/problems/big-countries/

我的解法:

select t1.name,t1.population,t1.area from world t1 where t1.area>3000000 or t1.population>25000000;

3.有趣的电影

https://leetcode-cn.com/problems/not-boring-movies/

我的解法:

select * from cinema t1 where t1.description != 'boring' and t1.id % 2 != 0 group by rating desc;

官方解法:

select * from cinema where mod(id, 2) = 1 and description != 'boring' order by rating DESC;

4.组合两个表

https://leetcode-cn.com/problems/combine-two-tables/

我的解法:

select t1.firstname,t1.lastname,t2.city,t2.state from person t1 left join address t2 on t1.personid = t2.personid;

这题就是考 left join的用法。

我一开始写错了。。写成join left 而且不知道要加on。。

5.交换工资

https://leetcode-cn.com/problems/swap-salary/

这题考的是  case then的用法。。但是我忘了。。就。。

官方解法:

update salary set sex = case sex when 'm' then 'f' else 'm' End;

6.超过经理收入的员工

https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/

我的解法:

select t1.name as Employee from Employee t1,Employee t2 where t1.managerid = t2.id and t1.salary > t2.salary;

别人的解法:

select t1.name Employee from Employee as t1 where salary > (select salary from Employee where id = t1.managerid);

学习一下嵌套查询。

7.从不订购的客户

https://leetcode-cn.com/problems/customers-who-never-order/

emm...基础太差。

用left join 代替 exists

别人的解法:

select t1.Name Customers from Customers t1 left join orders t2 on t1.id = t2.customerid where t2.id is null;

8.删除重复的电子邮箱

https://leetcode-cn.com/problems/delete-duplicate-emails/

看了下题解。。

解法:

delete t1 from person t1,person t2 where t1.email = t2.email and t1.id > t2.id;

9.上升的温度

https://leetcode-cn.com/problems/rising-temperature/

我的解法:

select t.id from weather t,weather t2 where t.RecordDate = t2.RecordDate + 1 and t.temperature > t2.temperature;

但是这个解法过不了。应该是时间计算的问题。

测试了一下,直接相减的值。

分析一下,估计是先转换成整数再相减,比如2019-6-17 14:33:37-2019-6-17 14:32:47 = 2019617143337-2019617143247 = 90。

怎么说呢,如果日期值是YYYY-MM-DD型 且同一个月的话 直接相减可以算天数。 但是还是用datediff靠谱。因为数据不可控。

然后看了下官解:

select t.id from weather t JOIN weather t2 ON  datediff(t.RecordDate,t2.RecordDate) = 1 and t.temperature > t2.temperature;

一个新认识的函数吧。datediff。

10.超过5名学生的课

https://leetcode-cn.com/problems/classes-more-than-5-students/

基础太差太差了。

解法:

select class from courses group by class having count(distinct student) > 4;

group by 用的不熟练 having 也不熟练 考虑的点不全面。

11.第二高的薪水

https://leetcode-cn.com/problems/second-highest-salary/

解法:

select (select distinct Salary from Employee group by Salary desc limit 1,1) as SecondHighestSalary;

这题我缺的几个点:

1.limit 1 offset 1(基础问题) limit 省略写法的话 第一个参数是偏移量 第二个参数是返回数量

2.分析问题,看了别人表示要去考虑相同第一的问题,因此加了个distinct。解决办法是自己想的,但是分析不是自己分析的。

3.无值时取null,再select一次。

目前都是简单难度的题。

接下来写中等难度的。

12.第N高的薪水

https://leetcode-cn.com/problems/nth-highest-salary/

解法一:

类似与上一题

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N = N-1;# 这个更改值要先改 不能再limit那里进行运算 那里只接受常数
RETURN (
  # Write your MySQL query statement below.
  select distinct salary from employee order by salary desc limit N, 1
);
END

解法二:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
  # Write your MySQL query statement below.
  select max(t1.Salary) from Employee t1
  where N-1 = (
    select count(distinct t2.Salary) from Employee t2 where t2.Salary > t1.Salary
  )
);
END

这个解法的意思呢就是 让N-1条t2数据都大于t1 然后返回剩下数据中的最大值

这个解法就是不用在BEGIN后写东西。但是效率好像不是很高。

顺便学习一下sql中函数的写法。

13.分数排名

https://leetcode-cn.com/problems/rank-scores/

解法:

select Score,(select count(distinct t2.Score) from Scores t2 where t2.score >= s.score) AS Rank from Scores s order by score desc;

要学会用count。

挺神奇的吧。

未完待续。

猜你喜欢

转载自www.cnblogs.com/lighter-jh/p/11038755.html