学习了mysql 查询变量@i:=@i+1,接下来进行强化练习。
1、查询表中⾄少连续三次的数字
1,建表
sql
DROP TABLE IF EXISTS numbers;
create table numbers(
id int not null auto_increment,
number int default 0,
primary key(id)
);
2,插入数据
sql
insert into numbers values
(1, 1),
(2, 1),
(3, 1),
(4, 1),
(5, 2),
(6, 3),
(7, 3),
(8, 3),
(9, 4),
(10, 5);
3,目标:
sql
SELECT * FROM numbers;
找出其中连续出现3次的数字:1,3
2、查询销售额较昨⽇上升的记录
1,建表
sql
DROP TABLE IF EXISTS sale;
create table sale(
id int not null AUTO_INCREMENT,
record_date date,
ammount int default 0,
primary key(id)
);
2,插入数据
sql
INSERT INTO sale VALUES
(1, '2020-01-01', 1000),
(2, '2020-01-02', 2500),
(3, '2020-01-03', 2000),
(4, '2020-01-04', 3000),
(5, '2020-01-05', 2900),
(6, '2020-01-06', 3100),
(7, '2020-01-07', 3300);
3,目标:
sql
SELECT * FROM sale;
从数据中找出销售额较昨⽇上升的记录,2、4、6、7
3、查询投票结果的排名情况
即,第一名、第二名是谁,排名情况。
1,建表
sql
DROP TABLE IF EXISTS vote;
create table vote(
id int not null auto_increment,
name varchar(30),
votes int default 0,
primary key(id)
);
2,插入数据
sql
insert into vote (id, name, votes) values
('1','name01','100'),
('2','name02','110'),
('3','name03','100'),
('4','name04','115'),
('5','name05','111'),
('6','name06','110'),
('7','name07','110'),
('8','name08','109'),
('9','name09','111');
3,目标:
sql
SELECT * FROM vote ORDER BY votes DESC;
对投票数进行标记排名情况
4、查询⽹站访问⾼峰期
目标: 查询网站访问高峰时期,高峰时期定义:至少连续三天访问量>=1000
1,建表
sql
DROP TABLE IF EXISTS visit_summary;
create table visit_summary(
id int not null auto_increment,
visit_date date,
visit_sum int default 0,
primary key(id)
);
2,插入数据
sql
insert into visit_summary values
(1, '2020-01-01', 300),
(2, '2020-01-02', 400),
(3 ,'2020-01-03', 500),
(4, '2020-01-04', 600),
(5, '2020-01-05', 700),
(6, '2020-01-06', 1000),
(7, '2020-01-07', 1100),
(8, '2020-01-08', 1200),
(9, '2020-01-09', 1300),
(10, '2020-01-10', 700),
(11, '2020-01-11', 1300),
(12, '2020-01-12', 1400),
(13 ,'2020-01-13', 1500),
(14, '2020-01-14', 900),
(15, '2020-01-15', 1100),
(16, '2020-01-16', 1200),
(17, '2020-01-17', 900),
(18, '2020-01-18', 1400),
(19, '2020-01-19', 1500),
(20, '2020-01-20', 1600),
(21, '2020-01-21', 1400),
(22, '2020-01-22', 1300),
(23 ,'2020-01-23', 1500),
(24, '2020-01-24', 900),
(25, '2020-01-25', 1100),
(26, '2020-01-26', 1200),
(27, '2020-01-27', 800);
总结:
学习后,通过具体的题目进行加强。第一次不会,就再看一次解答,理清楚思路。看看自己哪里卡住了。过个一两天,再来一遍,直到自己掌握了。
上一篇: 《mysql 查询变量@i:=@i+1》
下一篇: 《查询实战-变量方式-解答》