SQL刷题笔记

SQL刷题笔记

day1

2021/11/30

175.组合两个表

表1: Person

+-------------+---------+
| 列名         | 类型     |
+-------------+---------+
| PersonId    | int     |
| FirstName   | varchar |
| LastName    | varchar |
+-------------+---------+

PersonId 是上表主键
表2: Address

+-------------+---------+
| 列名         | 类型    |
+-------------+---------+
| AddressId   | int     |
| PersonId    | int     |
| City        | varchar |
| State       | varchar |
+-------------+---------+

AddressId 是上表主键

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

FirstName, LastName, City, State

解答:

# Write your MySQL query statement below
SELECT FirstName,LastName,City,State
FROM Person p
LEFT JOIN Address a
ON p.PersonId = a.PersonId;

总结:

多表的联结又分为以下几种类型

  • 左联结(left join),联结结果保留左表的全部数据
  • 右联结(right join),联结结果保留右表的全部数据
  • 内联结(inner join),取两表的公共数据

题目里要求“所有学生”,而“所有学生”在“学生”表里。如果有的学生没有选修课程,那么他就不会出现在“成绩”表里,所以“成绩”表没有包含“所有学生”。

所以要以“学生”表进行左联结,保留左边表(学生表)里的全部数据。

day2

2021/12/01

176.第二高的薪水

SQL架构

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

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

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

解答:

SELECT(
SELECT DISTINCT
    Salary
FROM Employee 
ORDER BY Salary DESC
LIMIT 1,1)
AS SecondHighestSalary

总结:

直接查询降序第二条数据即可,但是不存在第二条数据时,不会返回null。可以将其作为临时表使用。

解答2:

SELECT
IFNULL(
(SELECT DISTINCT
    Salary
FROM Employee 
ORDER BY Salary DESC
LIMIT 1 OFFSET 1),NULL)
AS SecondHighestSalary

总结:

使用IFNULL对查询结果进行判断。

177.第N高的薪水

SQL架构

编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

解答:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    SET N := N-1;
  RETURN (
      # Write your MySQL query statement below.
        SELECT
        IFNULL(
        (SELECT DISTINCT
            Salary
        FROM Employee 
        ORDER BY Salary DESC
        LIMIT 1 OFFSET N),NULL)
        AS SecondHighestSalary
        );
END

day3

2021/12/02

178. 分数排名

编写一个 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    |
+-------+------+

**重要提示:**对于 MySQL 解决方案,如果要转义用作列名的保留字,可以在关键字之前和之后使用撇号。例如 Rank

解答:

# Write your MySQL query statement below
SELECT Score,
(SELECT COUNT(DISTINCT Score) FROM Scores WHERE Score>=s.Score) AS `Rank`
FROM Scores s
ORDER BY Score DESC;

day5

2021/12/04

181. 超过经理收入的员工

SQL架构

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

+----------+
| Employee |
+----------+
| Joe      |
+----------+

解答:

# Write your MySQL query statement below
SELECT a.Name AS 'Employee'
FROM 
    Employee a,
    Employee b
WHERE 
    a.ManagerId = b.Id AND a.Salary > b.Salary

182. 查找重复的电子邮箱

SQL架构

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

示例:

+----+---------+
| Id | Email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+

根据以上输入,你的查询应返回以下结果:

+---------+
| Email   |
+---------+
| a@b.com |
+---------+

**说明:**所有电子邮箱都是小写字母。

解答1:

# Write your MySQL query statement below
SELECT DISTINCT a.Email   
FROM 
    Person a,
    Person b
WHERE 
    a.Email = B.Email AND a.Id != b.Id

解答2:

# Write your MySQL query statement below
SELECT Email   
FROM 
    Person 
GROUP BY Email
HAVING COUNT(Email)>1

183. 从不订购的客户

SQL架构

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+

Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+

例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

解答:

# Write your MySQL query statement below
SELECT c.name AS Customers
FROM Customers c
WHERE c.Id NOT IN(
	SELECT CustomerId FROM Orders
)

day6

2021/12/05

184. 部门工资最高的员工

SQL架构

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Jim   | 90000  | 1            |
| 3  | Henry | 80000  | 2            |
| 4  | Sam   | 60000  | 2            |
| 5  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门工资最高的员工。对于上述表,您的 SQL 查询应返回以下行(行的顺序无关紧要)。

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Jim      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

解释:

Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高。

解答:

# Write your MySQL query statement below
SELECT 
	Department.Name Department,
	Employee.Name Employee,
	Salary
FROM 
	Employee
LEFT JOIN
	Department ON Employee.DepartmentId = Department.Id
WHERE 
	(Employee.DepartmentId, Salary) IN 
	(SELECT DepartmentId, MAX(Salary)
    FROM Employee
    GROUP BY DepartmentId);

day7

2021/12/06

185. 部门工资前三高的所有员工

SQL架构

Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 85000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
| 7  | Will  | 70000  | 1            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 85000  |
| IT         | Will     | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

解释:

IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

解答:

# Write your MySQL query statement below
SELECT 
    d.Name Department, e1.Name Employee, e1.Salary
FROM 
    Employee e1
    JOIN
    Department d ON e1.DepartmentId = d.Id
WHERE
    3 > (SELECT
            COUNT(DISTINCT e2.Salary)
        FROM
            Employee e2
        WHERE
            e2.Salary > e1.Salary
            AND e1.DepartmentId = e2.DepartmentId
    )

day8

2021/12/07

196. 删除重复的电子邮箱

SQL架构

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Id 是这个表的主键。

例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+

提示:

  • 执行 SQL 之后,输出是整个 Person 表。
  • 使用 delete 语句。

解答:

# Write your MySQL query statement below
DELETE p1
FROM 
    Person p1,
    Person p2
WHERE
    p1.Email = p2.Email AND p1.Id > p2.Id

总结:

p1.Id > p2.Id

  • 从表p1取出3条记录;
  • 拿着第1条记录去表p2查找满足WHERE的记录,代入该条件p1.Email = p2.Email AND p1.Id > p2.Id后,发现没有满足的,所以不用删掉记录1;
  • 记录2同理;
  • 拿着第3条记录去表p2查找满足WHERE的记录,发现有一条记录满足,所以要从p1删掉记录3;
  • 3条记录遍历完,删掉了1条记录,这个DELETE也就结束了。

197. 上升的温度

SQL架构

Weather

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| recordDate    | date    |
| temperature   | int     |
+---------------+---------+
id 是这个表的主键
该表包含特定日期的温度信息

编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id

返回结果 不要求顺序

查询结果格式如下例:

Weather
+----+------------+-------------+
| id | recordDate | Temperature |
+----+------------+-------------+
| 1  | 2015-01-01 | 10          |
| 2  | 2015-01-02 | 25          |
| 3  | 2015-01-03 | 20          |
| 4  | 2015-01-04 | 30          |
+----+------------+-------------+

Result table:
+----+
| id |
+----+
| 2  |
| 4  |
+----+
2015-01-02 的温度比前一天高(10 -> 25)
2015-01-04 的温度比前一天高(20 -> 30)

解答:

# Write your MySQL query statement below
SELECT w2.Id
FROM Weather w1, Weather w2
WHERE DATEDIFF(w2.RecordDate, w1.RecordDate) = 1
AND w1.Temperature < w2.Temperature

总结:

DATEDIFF(d1,d2):日期1与日期2相差的天数。如果日期1比日期2大,结果为正;如果日期1比日期2小,结果为负。

day9

2021/12/08

595. 大的国家

SQL架构

这里有张 World

+-----------------+------------+------------+--------------+---------------+
| name            | continent  | area       | population   | gdp           |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |
| Albania         | Europe     | 28748      | 2831741      | 12960000      |
| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |
| Andorra         | Europe     | 468        | 78115        | 3712000       |
| Angola          | Africa     | 1246700    | 20609294     | 100990000     |
+-----------------+------------+------------+--------------+---------------+

如果一个国家的面积超过 300 万平方公里,或者人口超过 2500 万,那么这个国家就是大国家。

编写一个 SQL 查询,输出表中所有大国家的名称、人口和面积。

例如,根据上表,我们应该输出:

+--------------+-------------+--------------+
| name         | population  | area         |
+--------------+-------------+--------------+
| Afghanistan  | 25500100    | 652230       |
| Algeria      | 37100000    | 2381741      |
+--------------+-------------+--------------+

解答1:

# Write your MySQL query statement below
SELECT name, population, area
FROM World
WHERE population > 25000000 OR area > 3000000

解答2:

# Write your MySQL query statement below
SELECT name, population, area
FROM World
WHERE area > 3000000

UNION

SELECT name, population, area
FROM World
WHERE population > 25000000

day10

2021/12/09

596. 超过5名学生的课

SQL架构

有一个courses 表 ,有: student (学生)class (课程)

请列出所有超过或等于5名学生的课。

例如,表:

+---------+------------+
| student | class      |
+---------+------------+
| A       | Math       |
| B       | English    |
| C       | Math       |
| D       | Biology    |
| E       | Math       |
| F       | Computer   |
| G       | Math       |
| H       | Math       |
| I       | Math       |
+---------+------------+

应该输出:

+---------+
| class   |
+---------+
| Math    |
+---------+

提示:

  • 学生在每个课中不应被重复计算。

解答1:

# Write your MySQL query statement below
SELECT class
FROM (
	SELECT class, COUNT(DISTINCT student) num
    FROM courses
    GROUP BY class
) numtable
WHERE num>=5

解答2:

# Write your MySQL query statement below
SELECT class
FROM courses
GROUP BY class
HAVING COUNT(DISTINCT student) >= 5 

day11-递归 / 回溯

2021/12/10

17. 电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

img

示例 1:

输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]

示例 2:

输入:digits = ""
输出:[]

示例 3:

输入:digits = "2"
输出:["a","b","c"]

提示:

  • 0 <= digits.length <= 4
  • digits[i] 是范围 ['2', '9'] 的一个数字。

解答1:

class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> ans = new ArrayList<>();
        if(digits.length()==0) return ans;

        String[] s = new String[]{
                "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"
        };

        int t = digits.charAt(0)-'0';
        for(int i=0;i<s[t].length();i++){
            ans.add(String.valueOf(s[t].charAt(i)));
        }
        for(int i=1;i<digits.length();i++){
            t = digits.charAt(i)-'0';
            int m = ans.size();
            for(int j=0;j<m;j++) {
                for(int k=0;k<s[t].length();k++) {
                    StringBuilder ss = new StringBuilder();
                    ss.append(ans.get(j));
                    ss.append(String.valueOf(s[t].charAt(k)));
                    ans.add(ss.toString());
                }
            }
            for(int j=0;j<m;j++){
                ans.remove(0);
            }
        }
        return ans;
    }
}

解答2:

class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> ans = new ArrayList<>();
        if(digits.length()==0) return ans;
        String[] s = new String[]{
                "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"
        };
        fun(ans, s, digits, 0, new StringBuffer());
        return ans;
    }
    public void fun(List<String> ans, String[] s, String digits, int index, StringBuffer sb){
        if(index==digits.length()){
            ans.add(sb.toString());
        }else{
            char c = digits.charAt(index);
            String ss = s[c-'0'];
            System.out.println(ss);
            for (int i = 0; i < ss.length(); i++) {
                sb.append(ss.charAt(i));
                fun(ans, s, digits, index+1, sb);
                sb.deleteCharAt(index);
            }
        }
    }
}

22. 括号生成

数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。

示例 1:

输入:n = 3
输出:["((()))","(()())","(())()","()(())","()()()"]

示例 2:

输入:n = 1
输出:["()"]

提示:

  • 1 <= n <= 8

解答:

class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> ans = new ArrayList<>();
        if(n==0) return ans;
        fun(ans, n, 0, 0, new StringBuffer());
        return ans;
    }
    public void fun(List<String> ans, int n, int L, int R, StringBuffer sb){
        if(L==n&&R==n){
            ans.add(sb.toString());
        }else{
            if(L<n){
                sb.append("(");
                fun(ans, n, L+1, R, sb);
                sb.deleteCharAt(sb.length()-1);
            }
            if(R<n&&R<L){
                sb.append(")");
                fun(ans, n, L, R+1, sb);
                sb.deleteCharAt(sb.length()-1);
            }
        }
    }
}

79. 单词搜索

给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

示例 1:

img

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true

示例 2:

img

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:true

示例 3:

img

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:false

提示:

  • m == board.length
  • n = board[i].length
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • boardword 仅由大小写英文字母组成

**进阶:**你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?

解答:

class Solution {
    int[] a = {0,0,1,-1};
    int[] b = {1,-1,0,0};
    public boolean exist(char[][] board, String word) {
        int[][] vis = new int[board.length][board[0].length];
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                if(board[i][j]==word.charAt(0)){
                    vis[i][j]=1;
                    if(dfs(1,board,word,i,j,vis)){
                        return true;
                    }
                    vis[i][j]=0;
                }
            }
        }
        return false;
    }
    public boolean dfs(int n, char[][] board, String word, int x, int y,int[][] vis){
        if(n==word.length()){
            return true;
        }
        char c = word.charAt(n);
        boolean result = false;
        for(int i=0;i<4;i++){
            int xx=x+a[i];
            int yy=y+b[i];
            if(xx>=board.length||yy>=board[0].length||xx<0||yy<0||vis[xx][yy]==1) continue;
            if(board[xx][yy]==c){
                vis[xx][yy]=1;
                result = dfs(n+1,board,word,xx,yy,vis);
                if(result==true) break;
                vis[xx][yy]=0;
            }
        }
        return result;
    }
}

day11

2021/12/10

620. 有趣的电影

某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。

作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列。

例如,下表 cinema:

+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   1     | War       |   great 3D   |   8.9     |
|   2     | Science   |   fiction    |   8.5     |
|   3     | irish     |   boring     |   6.2     |
|   4     | Ice song  |   Fantacy    |   8.6     |
|   5     | House card|   Interesting|   9.1     |
+---------+-----------+--------------+-----------+

对于上面的例子,则正确的输出是为:

+---------+-----------+--------------+-----------+
|   id    | movie     |  description |  rating   |
+---------+-----------+--------------+-----------+
|   5     | House card|   Interesting|   9.1     |
|   1     | War       |   great 3D   |   8.9     |
+---------+-----------+--------------+-----------+

解答:

# Write your MySQL query statement below
SELECT *
FROM cinema
WHERE MOD(id,2) = 1 AND description != 'boring'
ORDER BY rating DESC

day12

2021/12/11

626. 换座位

SQL架构

小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。

其中纵列的 id 是连续递增的

小美想改变相邻俩学生的座位。

你能不能帮她写一个 SQL query 来输出小美想要的结果呢?

示例:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Abbot   |
|    2    | Doris   |
|    3    | Emerson |
|    4    | Green   |
|    5    | Jeames  |
+---------+---------+

假如数据输入的是上表,则输出结果如下:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Doris   |
|    2    | Abbot   |
|    3    | Green   |
|    4    | Emerson |
|    5    | Jeames  |
+---------+---------+

注意:

如果学生人数是奇数,则不需要改变最后一个同学的座位。

解答:

# Write your MySQL query statement below
SELECT
    IF(id % 2 = 0, id - 1,
        IF(id = (select count(id) from seat), id, id + 1)
    ) AS id, student 
FROM seat 
ORDER BY id

day13

2021/12/12

627. 变更性别

SQL架构

Salary 表:

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| id          | int      |
| name        | varchar  |
| sex         | ENUM     |
| salary      | int      |
+-------------+----------+
id 是这个表的主键。
sex 这一列的值是 ENUM 类型,只能从 ('m', 'f') 中取。
本表包含公司雇员的信息。

请你编写一个 SQL 查询来交换所有的 'f''m' (即,将所有 'f' 变为 'm' ,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表。

注意,你必须仅使用一条 update 语句,且 不能 使用 select 语句。

查询结果如下例所示:

Salary 表:
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1  | A    | m   | 2500   |
| 2  | B    | f   | 1500   |
| 3  | C    | m   | 5500   |
| 4  | D    | f   | 500    |
+----+------+-----+--------+

Result 表:
+----+------+-----+--------+
| id | name | sex | salary |
+----+------+-----+--------+
| 1  | A    | f   | 2500   |
| 2  | B    | m   | 1500   |
| 3  | C    | f   | 5500   |
| 4  | D    | m   | 500    |
+----+------+-----+--------+
(1, A) 和 (3, C) 从 'm' 变为 'f' 。
(2, B) 和 (4, D) 从 'f' 变为 'm' 。

解答:

# Write your MySQL query statement below
UPDATE salary
SET
	sex = CASE sex
        WHEN 'm' THEN 'f'
        ELSE 'm'
    END;

day14

2021/12/13

511. 游戏玩法分析 I

活动表 Activity:

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| player_id    | int     |
| device_id    | int     |
| event_date   | date    |
| games_played | int     |
+--------------+---------+
表的主键是 (player_id, event_date)。
这张表展示了一些游戏玩家在游戏平台上的行为活动。
每行数据记录了一名玩家在退出平台之前,当天使用同一台设备登录平台后打开的游戏的数目(可能是 0 个)。

写一条 SQL 查询语句获取每位玩家 第一次登陆平台的日期。

查询结果的格式如下所示:

Activity 表:

+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1         | 2         | 2016-03-01 | 5            |
| 1         | 2         | 2016-05-02 | 6            |
| 2         | 3         | 2017-06-25 | 1            |
| 3         | 1         | 2016-03-02 | 0            |
| 3         | 4         | 2018-07-03 | 5            |
+-----------+-----------+------------+--------------+

Result 表:

+-----------+-------------+
| player_id | first_login |
+-----------+-------------+
| 1         | 2016-03-01  |
| 2         | 2017-06-25  |
| 3         | 2016-03-02  |
+-----------+-------------+

解答:

SELECT player_id,MIN(event_date) first_login
FROM activity
GROUP BY player_id 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值