# 建表createtableifnotexists book
(
id intprimarykeyauto_incrementnotnull,
name varchar(20),
pub_date varchar(30),
price decimal(10,2));# 插入数据insertinto book(name, pub_date, price)values('射雕英雄传','1970-05-01 00:00:00',36.60),('天龙八部','1986-07-24 00:00:00 ',50.20),('笑傲江湖 ','1995-12-24 00:00:00',40.00),('雪山飞狐 ','1987-11-11 00:00:00',29.00);createtableifnotexists heroes
(
hid intprimarykeyauto_incrementnotnull,
name varchar(20),
age tinyint(4),
gender varchar(4),
skill varchar(20),
book_id int);insertinto heroes(name, age, gender, skill, book_id)values('郭靖',30,'男','降龙十八掌',1),('黄蓉',30,'女','打狗棍法 ',1),('黄药师',60,'男','弹指神通',1),('欧阳锋',65,'男','蛤蟆功',1),('梅超风 ',40,'女','九阴白骨爪',1),('乔峰 ',33,'男','降龙十八掌',2),('段誉 ',25,'男','六脉神剑',2),('虚竹 ',27,'男','天山六阳掌',2),('王语嫣 ',18,'女','神仙姐姐',2),('令狐冲',32,'男','独孤九剑',3),('任盈盈',24,'女','弹琴',3),('岳不群 ',50,'保密','华山剑法',3),('东方不败 ',99,'中性','葵花宝典',3),('胡斐',26,'男','胡家刀法',4),('苗若兰',16,'女','黄衣',4),('程灵素',20,'女','医术',4),('袁紫衣',22,'女','六合拳',4);
3、修改book表pub_date字段类型为date类型
altertable book
modify pub_date date;
4、查询所有的英雄信息以及对应的书名
select h.*, b.name
from heroes h
join book b on h.book_id = b.id;
5、查询80年代出版的书中所有的女性英雄信息以及对应的书的信息
select h.*, b.*from heroes h,
book b
where h.book_id = b.id
and gender ='女'and b.pub_date in(select b.pub_date
from book b
where b.pub_date >'1980-01-01 00:00:00');
6、查出会"降龙十八掌"的英雄名字以及对应的书名
select h.name, b.name
from heroes h,
book b
where h.skill ='降龙十八掌'and h.book_id = b.id;
7、查询每本书中英雄年龄的平均值
select b.name,avg(age)from heroes h,book b
where h.book_id = b.id
groupby h.book_id;
8、查询每本书中年纪最大的英雄
select b.name,avg(age)from heroes h,book b
where h.book_id = b.id
groupby h.book_id;select b.name 书名,h.name 人名
from heroes h,book b
where h.book_id = b.id and age =(selectmax(age)from heroes
)groupby h.book_id;select h.name as"英雄", h.age, b.name as"书名"from heroes as h
innerjoin(selectmax(age)as mage, book_id as bid from heroes groupby book_id)as m
innerjoin book as b on h.age = m.mage and m.bid = b.id;
createtableifnotexists worker
(
id intcomment'工号',
name varchar(10)comment'姓名',levelintcomment'能力值',
dep_id intcomment'所属部门')ENGINE=InnoDBDEFAULTCHARSET= utf8;
3. 创建一个 部门表department(部门编号id,部门名称name,主管工号mid)
createtableifnotexists department
(
id intcomment'部门编号',
name varchar(10)comment'部门名称',
mid intcomment'主管工号')ENGINE=InnoDBDEFAULTCHARSET= utf8;
update t_staff
set salary = salary +1000where salary <10000;
7. 删除工资大于30000 的员工
deletefrom t_staff
where salary >30000;
题目集四
createtableifnotexists students
(
studentNo intprimarykeyauto_incrementnotnull,
name varchar(10),
sex varchar(10),
hometown varchar(20),
age tinyint(4),
class_id intnotnull,
card varchar(20));insertinto students (name, sex, hometown, age, class_id, card)values('王昭君','女','北京',20,1,'340322199001247654'),('诸葛亮','男','上海',18,2,'340322199002242354'),('张飞','男','南京',24,3,'340322199003247654'),('白起','男','安徽',22,4,'340322199005247654'),('大乔','女','天津',19,3,'340322199004247654'),('孙尚香','女','河北',18,1,'340322199006247654'),('百里玄策','男','山西',20,2,'340322199007247654'),('小乔','女','河南',15,3,null),('百里守约','男','湖南',21,1,''),('妲己','女','广东',26,2,'340322199607247654'),('李白','男','北京',30,4,'340322199005267754'),('孙膑','男','新疆',26,3,'340322199000297655');
1. 查询学生"百里守约"的基本信息
select*from students
where name ='百里守约';
2. 查询学生"百里守约"或”百里玄策”的基本信息
select*from students
where name ='百里守约'or name ='百里玄策';
3. 查询姓"张"学生的姓名,年龄,班级
select name, age, class_id
from students
where name like'张%';
4. 查询姓名中含有"约"字的学生的基本信息
select*from students
where name like'%约%';
5. 查询姓名长度为三个字,姓“孙”的学生的学号,姓名,年龄,班级,身份证号
select studentNo, name, age, class_id, card
from students
where name like'孙__';
6. 查询姓"百"或者姓”孙”的学生的基本信息
select*from students
where name like'百%'or name like'孙%';
7. 查询姓"百"并且家乡是"山西"的学生信息
select*from students
where name like'百%'and hometown ='山西';
8. 查询家乡不是"北京"、“新疆”、“山东”、"上海"的学生的信息
select*from students
where hometown notin('北京','新疆','山东','上海');
9. 查询姓"孙",但是家乡不是"河北"的学生信息
select*from students
where name like'孙%'and hometown !='河北';
10. 查询家乡不是"北京"、“新疆”、“山东”、"上海"的学生的信息
select*from students
where hometown notin('北京','新疆','山东','上海');
11. 查询全部学生信息,并按照“性别”排序
select*from students
orderby sex;
12. 查询所有男生,并按年龄升序排序
select*from students
where sex ='男'orderby age;
13. 统计共有多少个学生
selectcount(*)from students;
14. 统计年龄大于20岁的学生有多少个
selectcount(*)from students
where age >20;
15. 统计男生的平均年龄
selectavg(age)from students
where sex ='男';
16. 查询1班学生中的最大年龄是多少
selectmax(age)from students
where class_id =1;
17. 统计2班男女生各有多少人
select sex,count(studentNo)from students
where class_id =2groupby sex;
18. 查询年龄最小的学生的全部信息
select*from students
where age =(selectmin(age)from students
);