当前位置: 移动技术网 > IT编程>数据库>Mysql > Leetcode 之 Mysql(day01)

Leetcode 之 Mysql(day01)

2019年10月27日  | 移动技术网IT编程  | 我要评论

  大四已经接近一半了,下学期就要准备找工作实习了。为了自己能找到一份比较满意的实习,今天开始要刷一下题目。今天就刷 mysql 语言。以下就是我今天刷的题目。大家也可以去 leetcode 注册一个账号来刷一下题目。里面有很多的算法题。

 

第一题:编写一个 sql 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:firstname, lastname, city, state

表1: person

+-------------+---------+
| 列名         | 类型     |
+-------------+---------+
| personid    | int     |
| firstname   | varchar |
| lastname    | varchar |
+-------------+---------+
personid 是上表主键

表2: address
+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| addressid   | int     |
| personid    | int     |
| city        | varchar |
| state       | varchar |
+-------------+---------+
addressid 是上表主键

select p.firstname, p.lastname, a.city, a.state 
from person p left join address a on p.personid = a.personid

  第一题比较简单,考查我们对两个表的连接,并且对于左连接这个概念。

 

第二题:编写一个 sql 查询,获取 employee 表中第二高的薪水(salary) 。

+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 employee 表,sql查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

+---------------------+
| secondhighestsalary |
+---------------------+
| 200                 |
+---------------------+

select ifnull(
    (select salary from employee 
     group by salary 
     order by salary desc 
     limit 1,1 ) , null) as secondhighestsalary

  第二题难点在于找不到的时候怎样返回一个 null 值。我就运用了 ifnull() 函数 。

  ifnull() 函数:用于判断第一个表达式是否为 null,如果为 null 则返回第二个参数的值,如果不为 null 则返回第一个参数的值。

  表达式:ifnull(expression, alt_value)。

  这样就很好解释第二题的 null 值了。这里还要掌握分组(group by) 和 排序(order by)。还有一个就是分页(limit)。

  这里就是通过工资分组,再排序,最后通过分页求出值。

 

第三题:编写一个 sql 查询,获取 employee 表中第 高的薪水(salary)。

create function getnthhighestsalary(n int) returns int
begin
    set n = n - 1;
  return (
      select ifnull(
          (select salary from employee
           group by salary 
          order by salary desc 
          limit n , 1) , null)
      as getnthhighestsalary
  );
end

  这题在第二题基础上修改成查找第n高的薪水。

  这里用函数的写法来考查我们。在第二题的基础上注意n的变量就可以求出这题了。

 

第四题:编写一个 sql 查询来实现分数排名。如果两个分数相同,则两个分数排名(rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

+----+-------+
| id | score |
+----+-------+
| 1  | 3.50  |
| 2  | 3.65  |
| 3  | 4.00  |
| 4  | 3.85  |
| 5  | 4.00  |
| 6  | 3.65  |
+----+-------+

例如,根据上述给定的 scores 表,你的查询应该返回(按分数从高到低排列):

+-------+------+
| score | rank |
+-------+------+
| 4.00  | 1    |
| 4.00  | 1    |
| 3.85  | 2    |
| 3.65  | 3    |
| 3.65  | 3    |
| 3.50  | 4    |
+-------+------+

 

select score , (select count(distinct score) from scores where score >= s.score) as rank
from scores s order by score desc

  此题难点在于怎样显示他们的排序。先统计有多少行,不能重复。

 

第五题:编写一个 sql 查询,查找所有至少连续出现三次的数字。

+----+-----+
| id | num |
+----+-----+
| 1  |  1  |
| 2  |  1  |
| 3  |  1  |
| 4  |  2  |
| 5  |  1  |
| 6  |  2  |
| 7  |  2  |
+----+-----+

例如,给定上面的 logs 表, 1 是唯一连续出现至少三次的数字。

+-----------------+
| consecutivenums |
+-----------------+
| 1               |
+-----------------+

 

select distinct(a.num) as consecutivenums 
    from logs a join logs b on a.id = b.id+1 join logs c on a.id = c.id + 2
    where a.num = b.num and a.num = c.num

  此题一定要注意题目是连续出现至少三次,要连续的。一开始我是直接统计出现的次数,再审题,发现不对。

  我就直接点,用表的自连接来解决这题。连接规则:根据id的连续三个以上来连接,再判断这三个值是否相等。

 

  这就是今天刷的题目,刚刚太久没有接触 sql 了,很多都不会写了。要经常锻炼才可以了。

  每天写一下博客,记录一下自己每天学到的知识。@hhh

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网