论述题 LeetCode180:连续出现的数字 题目链接:180. 连续出现的数字 - 力扣(Leetcode) 表:Logs +-------------+---------+ 编写一个 SQL 查询,查找所有至少连续出现三次的数字。 返回的结果表中的数据可以按 任意顺序 排列。 查询结果格式如下面的例子所示: 示例 1: 输入: 建表语句: Create table If Not Exists Logs (id int, num int)
| Column Name | Type |
+-------------+---------+
| id | int |
| num | varchar |
+-------------+---------+
id 是这个表的主键。
Logs 表:
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
输出:
Result 表:
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
解释:1 是唯一连续出现至少三次的数字。
Truncate table Logs
insert into Logs (id, num) values ('1', '1')
insert into Logs (id, num) values ('2', '1')
insert into Logs (id, num) values ('3', '1')
insert into Logs (id, num) values ('4', '2')
insert into Logs (id, num) values ('5', '1')
insert into Logs (id, num) values ('6', '2')
insert into Logs (id, num) values ('7', '2')
相关试题
简答题 面试题:MySQL中四种隔离级别分别是什么
论述题 ## LeetCode180:连续出现的数字 题目链接:[180. 连续出现的数字 - 力扣(Leetcode)](https://leetcode.cn/problems/consecutive-numbers/) 表:`Logs` ``` +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | num | varchar | +-------------+---------+ id 是这个表的主键。 ``` 编写一个 SQL 查询,查找所有至少连续出现三次的数字。 返回的结果表中的数据可以按 **任意顺序** 排列。 查询结果格式如下面的例子所示: **示例 1:** ``` 输入: Logs 表: +----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ 输出: Result 表: +-----------------+ | ConsecutiveNums | +-----------------+ | 1 | +-----------------+ 解释:1 是唯一连续出现至少三次的数字。 ``` 建表语句: ```mysql Create table If Not Exists Logs (id int, num int) Truncate table Logs insert into Logs (id, num) values ('1', '1') insert into Logs (id, num) values ('2', '1') insert into Logs (id, num) values ('3', '1') insert into Logs (id, num) values ('4', '2') insert into Logs (id, num) values ('5', '1') insert into Logs (id, num) values ('6', '2') insert into Logs (id, num) values ('7', '2') ```
简答题 面试题:SQL之连接查询(左连接和右连接的区别)
简答题 编程题:C# 连接Mysql进行常规增删查改数据
简答题 面试题:事务的特性有哪些?
简答题 面试题:SQL之sql注入是什么
简答题 面试题:数据库设计范式
论述题 ## LeetCode1393:股票的资本损益 题目链接:[1393. 股票的资本损益 - 力扣(Leetcode)](https://leetcode.cn/problems/capital-gainloss/) `Stocks` 表: ``` +---------------+---------+ | Column Name | Type | +---------------+---------+ | stock_name | varchar | | operation | enum | | operation_day | int | | price | int | +---------------+---------+ (stock_name, day) 是这张表的主键 operation 列使用的是一种枚举类型,包括:('Sell','Buy') 此表的每一行代表了名为 stock_name 的某支股票在 operation_day 这一天的操作价格。 保证股票的每次'Sell'操作前,都有相应的'Buy'操作。 ``` 编写一个SQL查询来报告每支股票的资本损益。 股票的资本损益是一次或多次买卖股票后的全部收益或损失。 以任意顺序返回结果即可。 SQL查询结果的格式如下例所示: ``` Stocks 表: +---------------+-----------+---------------+--------+ | stock_name | operation | operation_day | price | +---------------+-----------+---------------+--------+ | Leetcode | Buy | 1 | 1000 | | Corona Masks | Buy | 2 | 10 | | Leetcode | Sell | 5 | 9000 | | Handbags | Buy | 17 | 30000 | | Corona Masks | Sell | 3 | 1010 | | Corona Masks | Buy | 4 | 1000 | | Corona Masks | Sell | 5 | 500 | | Corona Masks | Buy | 6 | 1000 | | Handbags | Sell | 29 | 7000 | | Corona Masks | Sell | 10 | 10000 | +---------------+-----------+---------------+--------+ Result 表: +---------------+-------------------+ | stock_name | capital_gain_loss | +---------------+-------------------+ | Corona Masks | 9500 | | Leetcode | 8000 | | Handbags | -23000 | +---------------+-------------------+ Leetcode 股票在第一天以1000美元的价格买入,在第五天以9000美元的价格卖出。资本收益=9000-1000=8000美元。 Handbags 股票在第17天以30000美元的价格买入,在第29天以7000美元的价格卖出。资本损失=7000-30000=-23000美元。 Corona Masks 股票在第1天以10美元的价格买入,在第3天以1010美元的价格卖出。在第4天以1000美元的价格再次购买,在第5天以500美元的价格出售。最后,它在第6天以1000美元的价格被买走,在第10天以10000美元的价格被卖掉。资本损益是每次(’Buy'->'Sell')操作资本收益或损失的和=(1010-10)+(500-1000)+(10000-1000)=1000-500+9000=9500美元。 ``` 建表语句: ```mysql Create Table If Not Exists Stocks (stock_name varchar(15), operation ENUM('Sell', 'Buy'), operation_day int, price int) Truncate table Stocks insert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Buy', '1', '1000') insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '2', '10') insert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Sell', '5', '9000') insert into Stocks (stock_name, operation, operation_day, price) values ('Handbags', 'Buy', '17', '30000') insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '3', '1010') insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '4', '1000') insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '5', '500') insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '6', '1000') insert into Stocks (stock_name, operation, operation_day, price) values ('Handbags', 'Sell', '29', '7000') insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '10', '10000') ```