MySQL基础
1、如果暂停或开启MySQL服务?
net stop mysql80(版本号)
net start mysql80(版本号)
2、如何使用cmd命令登录MySQL窗口
mysql -u 用户名 -p 密码
3、登录时用到-u \ -p等参数,如何查看所有参数意思?
mysql --help
MySQL练习题
习题
学生表:students
Column Name | Type |
---|---|
student_id | int |
student_name | varchar |
创建表
create table if not exists Students (student_id int primary key,student_name varchar(20));
插入数据
Truncate table students;
insert into students (student_id, student_name) values ('1', 'Alice');
insert into students (student_id, student_name) values ('2', 'Bob');
insert into students (student_id, student_name) values ('13', 'John');
insert into students (student_id, student_name) values ('6', 'Alex');
科目表:subjects
Column Name | Type |
---|---|
subject_name | varchar |
创建表
create table if not exists subjects (subject_name varchar(20) primary key);
插入数据
Truncate table Subjects;
insert into subjects (subject_name) values ('Math');
insert into subjects (subject_name) values ('Physics');
insert into subjects (subject_name) values ('Programming');
考试表:examinations
Column Name | Type |
---|---|
student_id | int |
subject_name | varchar |
创建表
create table if not exists examinations (student_id int, subject_name varchar(20));
插入数据
Truncate table Examinations;
insert into examinations (student_id, subject_name) values ('1', 'Math');
insert into examinations (student_id, subject_name) values ('1', 'Physics');
insert into examinations (student_id, subject_name) values ('1', 'Programming');
insert into examinations (student_id, subject_name) values ('2', 'Programming');
insert into examinations (student_id, subject_name) values ('1', 'Physics');
insert into examinations (student_id, subject_name) values ('1', 'Math');
insert into examinations (student_id, subject_name) values ('13', 'Math');
insert into examinations (student_id, subject_name) values ('13', 'Programming');
insert into examinations (student_id, subject_name) values ('13', 'Physics');
insert into examinations (student_id, subject_name) values ('2', 'Math');
insert into examinations (student_id, subject_name) values ('1', 'Math');
要求写一段SQL,查询出每个学生参加每一门科目测试的次数,结果按student_id和subject_name排序。
select
a.student_id,
a.student_name,
b.subject_name,
count(c.subject_name) as attend_exams
from students as a
join subjects as b
left join examinations as c
on a.student_id=c.student_id and b.subject_name=c.subject_name
group by a.student_id,a.student_name,b.subject_name
order by a.student_id,b.subject_name