MySQL常用SQL语句2
-- 1创建student和score表 CREATE TABLE Student( Id INT(10) PRIMARY KEY auto_increment, Name VARCHAR(20) NOT NULL, Sex VARCHAR(4), Birth YEAR, Department VARCHAR(20), Address VARCHAR(50) ) CREATE TABLE Score( Id INT(10) PRIMARY KEY auto_increment, Stu_id INT(10) NOT NULL, C_name VARCHAR(20), Grade INT(10) ) -- 2.为student表和score表增加记录 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); -- 外键约束(数据库为了保持数据的完整性正确性,通常在有关系两个表中会建立外键约束) -- 就这个案例而言[score中的 Stu_id实际引用了student中的id] -- 所有的约束都是数据库对自身的保护 -- CONSTRAINT '1' FOREIGN KEY(stu_id) REFERENCES student(id) ALTER TABLE score ADD FOREIGN KEY(Stu_id) REFERENCES student(Id);-- ALTER更改表结构 UPDATE更新表数据 -- 两表查询 -- 笛卡尔积(存在大量无效数据) SELECT * FROM student,score; SELECT * FROM student,score WHERE score.Stu_id=student.Id;-- 正确 查询后让主外键对应 -- 内连接(专业)【只会显示两表匹配的信息】 SELECT * FROM student INNER JOIN score ON score.Stu_id=student.Id; -- 外连接 -- 就让两个表产生了级别差异,以某个表为主表,主表信息完全展示,从表信息只显示与主表匹配信息 SELECT * FROM student LEFT JOIN score ON score.Stu_id=student.Id;-- 外连接【左外连接(以左表为主表,将左表所有信息都显示出来,右表不匹配的显示为空),右外连接】 SELECT * FROM student RIGHT JOIN score ON score.Stu_id=student.Id;-- 因为score表有外键,所以信息是一一对应的 -- 子查询(一般是由多个查询组合而成,内层查询的结果可以作为外层查询中的一个条件) -- 查询比aa同学年龄大的学生 -- 步骤1 先查aa的年龄 SELECT age FROM user WHERE NAME='aa';-- 查询是有一个结果的 age=20 -- 步骤2 查询其他学生信息与 刚才的结果比较 SELECT * from user WHERE aeg>(SELECT aeg from user WHERE name='aa') SELECT * from user WHERE id IN(SELECT id from user WHERE id BETWEEN 3 and 8) -- 索引 触发器 -- 3.查询student表的所有记录 SELECT * FROM student; -- 4.查询student表的第2条到4条记录 SELECT * FROM student LIMIT 1,3; -- 5从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息 SELECT Id,NAME,Department FROM student; -- 6.从student表中查询计算机系和英语系的学生的信息 SELECT * FROM student WHERE Department='计算机系' OR Department='英语系'; SELECT * FROM student WHERE Department IN('计算机系','英语系'); -- 7.从student表中查询年龄18~22岁的学生信息 -- 获取年份 -- select YEAR(CURDATE()); -- -- 2018 -- 获取月份 -- -- select MONTH(CURDATE()); -- -- -- 获取日期 -- -- select DATE(CURDATE()); -- -- 2018-04-22 -- 获取当年一月份 -- -- select CONCAT(YEAR(CURDATE()),'-','01'); -- -- 2018-01 SELECT YEAR(CURDATE());-- 获取当前年份 SELECT * FROM student WHERE YEAR(CURDATE())-Birth>=28 AND YEAR(CURDATE())-Birth<=32; SELECT * FROM student WHERE DATE_FORMAT(NOW(),'%Y')-Birth BETWEEN 28 AND 32;-- 问题 -- 8.从student表中查询每个院系有多少人 SELECT Department AS '院系名称',COUNT(*) AS '总数'FROM student GROUP BY Department; -- 9.从score表中查询每个科目的最高分 SELECT C_name,MAX(Grade) FROM score GROUP BY C_name; -- 10.查询李四的考试科目(c_name)和考试成绩(grade) SELECT C_name,grade FROM score WHERE Stu_id=(SELECT Id FROM student WHERE NAME='李四'); -- 11.用连接的方式查询所有学生的信息和考试信息 SELECT student.*,score.C_name,score.Grade FROM student INNER JOIN score ON student.Id=score.Stu_id; -- 12.计算每个学生的总成绩 -- SELECT student.`Name`,SUM(grade) FROM student JOIN score ON student.Id=score.Stu_id SELECT Stu_id AS '学号',student.name AS '姓名',SUM(grade) AS '总成绩' FROM score JOIN student ON student.Id=score.Stu_id GROUP BY Stu_id;-- 刚好一个id对应一个名字 -- 13.计算每个考试科目的平均成绩 SELECT C_name,AVG(Grade) from score GROUP BY C_name; -- 14.查询计算机成绩低于95的学生信息 SELECT * FROM student WHERE Id IN(SELECT stu_id FROM score WHERE C_name='计算机' AND Grade<95); -- 15.查询同时参加计算机和英语考试的学生的信息 -- SELECT * FROM student WHERE Id IN(SELECT stu_id FROM score WHERE C_name='计算机' ) UNION SELECT * FROM student WHERE Id IN(SELECT stu_id FROM score WHERE C_name='英语' ); SELECT * FROM student WHERE Id IN ( SELECT stu_id FROM score WHERE c_name='计算机' AND stu_id IN (SELECT stu_id FROM score WHERE c_name='英语') ) -- 16.将计算机考试成绩按从高到低进行排序 SELECT * FROM score WHERE C_name='计算机' ORDER BY grade DESC; -- 17.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩 SELECT student.`Name`,student.Department,GROUP_CONCAT(score.C_name),GROUP_CONCAT(score.Grade) FROM student JOIN score ON student.Id=score.Stu_id WHERE student.`Name` LIKE '张%' OR student.`Name` LIKE '王%' GROUP BY student.`Name`; -- 18.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩 SELECT student.`Name`,(YEAR(CURDATE())-student.Birth)AS age,student.Department,score.C_name,score.Grade FROM student JOIN score ON student.Id=score.Stu_id WHERE student.Address LIKE '湖南%';