9 min to read
MySQL练习
遇到的问题
Qustion01: 1366 - Incorrect string value
Solution:
SET NAMES 'utf8'; SET CHARACTER SET utf8;
示例
创建表格
use login;
-- 学生表
CREATE TABLE `student`(
`s_id` int(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '' ,
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
-- 课程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
-- 教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
-- 成绩表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
-- 插入学生表测试数据
insert into student values('01' , '赵雷' , '1990-01-01' , '男');
insert into student values('02' , '钱电' , '1990-12-21' , '男');
insert into student values('03' , '孙风' , '1990-05-20' , '男');
insert into student values('04' , '李云' , '1990-08-06' , '男');
insert into student values('05' , '周梅' , '1991-12-01' , '女');
insert into student values('06' , '吴兰' , '1992-03-01' , '女');
insert into student values('07' , '郑竹' , '1989-07-01' , '女');
insert into student values('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
语法
-- 查询表格字段属性
desc tablename;
show full columns from table_name;
-- 查询表格的前五行
SELECT * FROM table_name LIMIT row_count;
-- 删除表
drop table table_name;
-- 清空表数据
truncate table table_name;
-- 删除表数据
DELETE FROM table_name
WHERE condition;
-- where和having区别
having只能用在group by之后,对分组后的结果进行筛选(即使用having的前提条件是分组)。
where肯定在group by 之前。
-- 修改数据
UPDATE table_name SET col_name = value WHERE condition;
练习
-- 查询平均成绩大于60分的同学的学号和平均成绩
select s_id,avg(s_score) from Score
group by s_id
having AVG(s_score)>60;
-- 查询所有同学的学号、姓名、选课数、总成绩
select Score.s_id,S_name,count(*),sum(s_score) from Score,Student where Score.s_id=Student.s_id group by Score.s_id;
-- 查询姓“李”的老师的个数;
select count(distinct(T_name)) from Teacher where T_name like '李%';
-- 查询没学过“李四”老师课的同学的学号、姓名
select S_id,S_name from Student where S_id not in (
select S_id from Score where C_id in (
select C_id from Course where T_id = (
select T_id from Teacher where T_name ='李四')));
select s_id,S_name from Student where s_id not in(
select Score.s_id from Score inner join Course on Score.c_id=Course.c_id inner join Teacher on Course.t_id=Teacher.t_id where t_name='李四');
-- 查询“01”课程比“02”课程成绩高的所有学生的学号
select a.s_id from
(select s_id,S_score from Score where c_id='01') a inner join
(select s_id,S_score from Score where c_id='02') b
on a.s_id=b.s_id
where a.S_score>b.S_score;
select s_id from Score a
where c_id= '01' and
S_score > (select S_score from Score b where c_id='02'and a.s_id=b.s_id); #“>”的优先级大于“=”
-- 查询学过“01”并且也学过编号“02”课程的同学的学号、姓名
select s_id,S_name from Student where s_id in(
select s_id from Score where c_id = '01' and s_id in(
select s_id from Score where c_id='02'));
-- 查询与“03”同学所学课程相同的其他同学学号和姓名
select s_id,S_name from Student where s_id in(
select s_id from Score where c_id in (select c_id from Score where s_id='03')
group by s_id
having count(*) = (select count(*) from Score where s_id='03')
);
-- 按平均成绩从高到低显示所有学生的“英语”、“语文”、“数学”三门的课程成绩,按如下形式显示: 学生ID,,语文,数学,英语,有效课程数,有效平均分。
select s_id as '学生ID',
(select S_score from Score a where c_id =(select c_id from Course where C_name='语文') and a.s_id=SC.s_id) as '语文',
(select S_score from Score b where c_id =(select c_id from Course where C_name='数学') and b.s_id=SC.s_id) as '数学',
(select S_score from Score c where c_id =(select c_id from Course where C_name='英语') and c.s_id=SC.s_id) as '英语',
count(*) as '有效课程数',
avg(S_score) as '有效平均分'
from Score SC group by s_id
order by avg(S_score) desc;
参考资料
[1]. MySQL查询语句练习题(50题版)
[2]. sql语句练习50题(Mysql版)
Comments