1.创建一个名为mydb的数据库
create database mydb default character set 'utf8';
2.进入mydb
use mydb;
13:55 2018/5/29
3.创建student表
create table student(
stuid int auto_increment primary key,
name varchar(20) not null,
age int,
score float not null,
note text
);
4.向student表中插入一条记录
insert into student(name,age,score,note)values('风清扬',20,93.5,'风清扬独孤九剑');
插入成功之后,查询表的所有记录:
select * from student;
insert into student values(23,'令狐冲',18,65.5,'家住华山');
insert into student(name,age,score)values('郭靖',25,62),('黄蓉',24,73.5),
('马云',40,79.5);
5.把学生表中所有的记录的成绩score改为60分
update student set score=60;
6.将姓名为“令狐冲”的记录的成绩改为75分
update student set score=75 where name='令狐冲';
7.删除学号为27的学生记录
delete from student where stuid=27;
8.查询所有学生的姓名和成绩,并以别名的形式显示字段
select name as 姓名,score as 成绩 from student;
9.查询出所有成绩在85到95之间的学生记录
select * from student where score >=85 and score <= 95;
或者
select * from student where score between 85 and 95;
10.查询所有备注信息不为null的学生记录
select * from student where note is not null;
11.查询学生表中的前三条记录
select * from student limit 3;
12.查询成绩大于等于70分的学生中,前两条记录
select * from student where score >= 70 limit 2;
13.查询学生表中第三条与第四条记录
select * from student limit 2,2;
14.假如每页最多显示2条记录,查询第三页应该显示的记录
select * from student limit 4,2;
15.按照成绩从高到低,将学生排序
select * from student order by score desc;
16.按照成绩从高到低,将学生排序,如果成绩相等则再按照年龄降序排序
select * from student order by score desc,age desc;
===========================以下为分组查询相应的SQL===========================
17.创建商品表product,并添加记录
create table product(
proid int auto_increment primary key,
proname varchar(20) not null,
price float not null,
type varchar(20) not null,
note text
);
insert into product(proname,price,type,note)values('锅巴',5,'零食',null),
('洗衣粉',8,'日用品',null),('可口可乐',12,'饮料','新品上市的Cola'),
('辣条',2,'零食',null),('脸盆',10,'日用品',null),('薯片',13,'零食',null),
('脉动',6,'饮料',null);
18.按照商品类型type对product表中的记录进行分组,求出每一组的平均价格
select type as 类型,avg(price) as 平均价格 from product group by type;
19.按照商品类型type对product表中的记录进行分组,求出零食组的平均价格
select type as 类型,avg(price) as 平均价格 from product
group by type having type='零食';
create database mydb default character set 'utf8';
2.进入mydb
use mydb;
13:55 2018/5/29
3.创建student表
create table student(
stuid int auto_increment primary key,
name varchar(20) not null,
age int,
score float not null,
note text
);
4.向student表中插入一条记录
insert into student(name,age,score,note)values('风清扬',20,93.5,'风清扬独孤九剑');
插入成功之后,查询表的所有记录:
select * from student;
insert into student values(23,'令狐冲',18,65.5,'家住华山');
insert into student(name,age,score)values('郭靖',25,62),('黄蓉',24,73.5),
('马云',40,79.5);
5.把学生表中所有的记录的成绩score改为60分
update student set score=60;
6.将姓名为“令狐冲”的记录的成绩改为75分
update student set score=75 where name='令狐冲';
7.删除学号为27的学生记录
delete from student where stuid=27;
8.查询所有学生的姓名和成绩,并以别名的形式显示字段
select name as 姓名,score as 成绩 from student;
9.查询出所有成绩在85到95之间的学生记录
select * from student where score >=85 and score <= 95;
或者
select * from student where score between 85 and 95;
10.查询所有备注信息不为null的学生记录
select * from student where note is not null;
11.查询学生表中的前三条记录
select * from student limit 3;
12.查询成绩大于等于70分的学生中,前两条记录
select * from student where score >= 70 limit 2;
13.查询学生表中第三条与第四条记录
select * from student limit 2,2;
14.假如每页最多显示2条记录,查询第三页应该显示的记录
select * from student limit 4,2;
15.按照成绩从高到低,将学生排序
select * from student order by score desc;
16.按照成绩从高到低,将学生排序,如果成绩相等则再按照年龄降序排序
select * from student order by score desc,age desc;
===========================以下为分组查询相应的SQL===========================
17.创建商品表product,并添加记录
create table product(
proid int auto_increment primary key,
proname varchar(20) not null,
price float not null,
type varchar(20) not null,
note text
);
insert into product(proname,price,type,note)values('锅巴',5,'零食',null),
('洗衣粉',8,'日用品',null),('可口可乐',12,'饮料','新品上市的Cola'),
('辣条',2,'零食',null),('脸盆',10,'日用品',null),('薯片',13,'零食',null),
('脉动',6,'饮料',null);
18.按照商品类型type对product表中的记录进行分组,求出每一组的平均价格
select type as 类型,avg(price) as 平均价格 from product group by type;
19.按照商品类型type对product表中的记录进行分组,求出零食组的平均价格
select type as 类型,avg(price) as 平均价格 from product
group by type having type='零食';