Lev1

导航

mysql练习

CREATE TABLE student(
    Id INT(10) NOT null PRIMARY KEY AUTO_INCREMENT COMMENT '学号',
    `name` VARCHAR(20) NOT NULL COMMENT '姓名',
    SEX VARCHAR(4) COMMENT '学性别',
    Birth YEAR COMMENT '出生年月',
    Department VARCHAR(20) NOT NULL COMMENT '院系',
    Address VARCHAR(21) COMMENT '家庭住址',
    UNIQUE KEY(Id)
)
CREATE TABLE Score(
    Id INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '编号',
    stu_id INT(10) not NULL
    COMMENT '学号',
    c_name VARCHAR(20) COMMENT '课程名',
    grade INT(10) COMMENT '分数',
    UNIQUE KEY(Id)
)
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);
#
SELECT * FROM student WHERE Id BETWEEN 902 AND 904;
SELECT Id,`name`,Department FROM student;

SELECT Id,`name`,Department FROM student 
where Department = '计算机系' OR Department = '英语系';
#
SELECT department, COUNT(id) FROM student GROUP BY department;
SELECT c_name,MAX(grade) FROM score GROUP BY
c_name;


SELECT t.name,e.c_name FROM student as t,score as e WHERE t.Id =e.stu_id AND t.name='李四';

SELECT t.name,t.department ,e.c_name,e.grade from student as t,score as e where t.Id =e.stu_id;

SELECT t.name as '姓名',SUM(grade) as '总成绩' FROM student as t,score as e 
WHERE t.Id =e.stu_id GROUP BY t.Id;

SELECT c_name as '姓名',AVG(grade) as '平均分'FROM score grade GROUP BY c_name;

SELECT `name`,Sex,birth,Department,address FROM student t, score e WHERE t.Id=e.stu_id AND e.c_name='计算机' AND grade <95;

SELECT * FROM student WHERE Id in(
SELECT stu_id FROM score where stu_id
(SELECT stu_id FROM score WHERE c_name='计算机') and c_name='英语');

select t.`name`,e.grade FROM student t,score e WHERE t.Id=e.stu_id  and e.c_name='计算机' ORDER BY(GRADE) DESC; 
SELECT C_NAME '科目',GRADE '考试成绩' FROM SCORE WHERE C_NAME='计算机' ORDER BY(GRADE) DESC;

SELECT DISTINCT `name` as '姓名',t.ID+STU_ID '学号' FROM student t,score e WHERE t.ID=e.STU_ID;

SELECT `name`,Department,c_name,grade FROM
student as t,score as e WHERE t.Id=e.stu_id AND `name` LIKE '张%';

SELECT `name`,Birth,Address,Department,c_name,grade
FROM student as t,score as e WHERE
 t.Id=e.stu_id AND Address like '湖南%';

 

posted on 2019-08-18 20:16  Lev1  阅读(138)  评论(0编辑  收藏  举报