1. MYSQL安装
安装教程:没有data目录和my-default.ini和my.ini文件以及服务无法启动的解决办法以及修改初始密码的方法
2. Mysql 基础语法以及常用的命令语句
需要注意:SQL 对大小写不敏感:SELECT 与 select 是相同的
(1)提取、更新与删除
SELECT - 从数据库中提取数据 ###:相当于tag,选取所需“数据” /// +where 限制条件
UPDATE - 更新数据库中的数据 ###更新数据需要进行限定where,不然就是全局更改
DELETE - 从数据库中删除数据 ### 限制性类同于update;不删除表的情况下,删除表中所有的行:
DELETE FROMtable_name; 或 DELETE *FROMtable_name;
延伸: SQL关于删除的三个语句:DROP、TRUNCATE、DELETE的区别。
a. DROP:
DROP test; 删除表test,并释放空间,将test删除的一干二净。
b. TRUNCATE:
TRUNCATE test; 删除表test里的内容,并释放空间,但不删除表的定义,表的结构还在。
c. DELETE:
1、删除指定数据
删除表test中年龄等于30的且国家为US的数据
DELETE FROM test WHERE age=30 AND country='US';
2、删除整个表
仅删除表test内的所有内容,保留表的定义,不释放空间。
DELETE FROM test 或者 DELETE FROM test;DELETE * FROM test 或者 DELETE * FROM test;
(2). 常用的插入查询
INSERT INTO - 向数据库中插入新数据
INSERT INTO Websites (name, url, alexa, country)
VALUES ('百度','https://www.baidu.com/','4','CN');
CREATE DATABASE - 创建新数据库
ALTER DATABASE - 修改数据库
CREATE TABLE - 创建新表
ALTER TABLE - 变更(改变)数据库表
DROP TABLE - 删除表
CREATE INDEX - 创建索引(搜索键)
DROP INDEX - 删除索引
***ORDER BY - 关键字用于对结果集按照一个列或者多个列进行排序。
如果需要按照 降序 对记录进行排序,您可以使用 DESC 关键字
eg : order by A,B 这个时候都是默认按升序排列
order by A desc,B 这个时候 A 降序,B 升序排列
order by A ,B desc 这个时候 A 升序,B 降序排列
(3). 相关练习
(其中部分相近的会写在一块,顺序可能会有些乱,需要后续的整理和继续学习实践)
1.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select a.* from
student a,score b,score c
where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02';
*** 两种完全不同的思路
2. 查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select a.* from
student a
where a.s_id in (select s_id from score where c_id='01' ) and a.s_id not in(select s_id from score where c_id='02')
3. 自己想想这个功能是什么
SELECT s.* FROM
student s WHERE s.s_id IN(
SELECT s_id FROM score WHERE s_id NOT IN(
SELECT a.s_id FROM score a
JOIN score b ON a.s_id = b.s_id AND b.c_id='02'
JOIN score c ON a.s_id = c.s_id AND c.c_id='03'
WHERE a.c_id='01'))
4. 查询和"01"号的同学学习的课程完全相同的其他同学的信息;
最后步使用统计量count
SELECT a.* FROM student a WHERE a.s_id IN (
SELECT DISTINCT s_id FROM score WHERE s_id!="01" AND c_id IN (
SELECT c_id FROM score WHERE s_id="01") GROUP BY s_id
HAVING COUNT(1)=(SELECT COUNT(1) FROM score WHERE s_id="01"))
5. 核心在于having 和where的位置
查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT a.`s_id`, a.`s_name`, ROUND(AVG(b.s_score)) AS avg_score FROM
student a
LEFT JOIN score b ON a.`s_id`=b.s_id
WHERE a.`s_id` IN (
SELECT s_id FROM score WHERE s_score<60 GROUP BY s_id HAVING COUNT(1)>=2)
GROUP BY a.`s_id`, a.`s_name`
6. 此处的知识点在于选取时 可将(语句)作为属性
按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT a.s_id,(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='01') AS 语文,
(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='02') AS 数学,
(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='03') AS 英语,
ROUND(AVG(s_score),2) AS 平均分
FROM score a
GROUP BY a.s_id ORDER BY 平均分 DESC;
7.(19)排序,需要后续进行研究
-- 24、查询学生平均成绩及其名次
SELECT a.s_id,
@i:=@i+1 AS '不保留空缺排名',
@k:=(CASE WHEN @avg_score=a.avg_s THEN @k ELSE @i END) AS '保留空缺排名',
@avg_score:=avg_s AS '平均分'
FROM (SELECT s_id,ROUND(AVG(s_score),2) AS avg_s FROM score GROUP BY s_id)a,(SELECT @avg_score:=0,@i:=0,@k:=0)b;
-- 25、查询各科成绩前三名的记录
-- 1.选出b表比a表成绩大的所有组
-- 2.选出比当 前id成绩大的 小于三个的
SELECT a.s_id,a.c_id,a.s_score FROM score a
LEFT JOIN score b ON a.c_id = b.c_id AND a.s_score GROUP BY a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3 ORDER BY a.c_id,a.s_score DESC 8. 一个是union 的连续使用;还发现另一个问题(尝试找出): SELECT a.*, b.rank,b.s_score, b.c_id FROM ( SELECT a.s_id,a.s_score,a.c_id,@k:=@k+1 AS rank FROM score a,(SELECT @k:=0)s WHERE a.c_id='03' )b LEFT JOIN student a ON b.`s_id`=a.`s_id` WHERE rank BETWEEN 2 AND 3 查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 SELECT d.*,c.排名,c.s_score,c.c_id FROM ( SELECT a.s_id,a.s_score,a.c_id,@i:=@i+1 AS 排名 FROM score a,(SELECT @i:=0)s WHERE a.c_id='01' )c LEFT JOIN student d ON c.s_id=d.s_id WHERE 排名 BETWEEN 2 AND 3 UNION SELECT d.*,c.排名,c.s_score,c.c_id FROM ( SELECT a.s_id,a.s_score,a.c_id,@j:=@j+1 AS 排名 FROM score a,(SELECT @j:=0)s WHERE a.c_id='02' )c LEFT JOIN student d ON c.s_id=d.s_id WHERE 排名 BETWEEN 2 AND 3 UNION SELECT d.*,c.排名,c.s_score,c.c_id FROM ( SELECT a.s_id,a.s_score,a.c_id,@k:=@k+1 AS 排名 FROM score a,(SELECT @k:=0)s WHERE a.c_id='03' )c LEFT JOIN student d ON c.s_id=d.s_id WHERE 排名 BETWEEN 2 AND 3; 9. 多条件时的执行顺序 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 SELECT c_id,ROUND(AVG(s_score),2) AS avg_score FROM score GROUP BY c_id ORDER BY avg_score DESC,c_id ASC 10.排序提取 查询每门功成绩最好的前两名 -- 牛逼的写法 SELECT a.s_id,a.c_id,a.s_score FROM score a WHERE (SELECT COUNT(1) FROM score b WHERE b.c_id=a.c_id AND b.s_score>=a.s_score)<=2 ORDER BY a.c_id 11.日期 查询各学生的年龄 -- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 SELECT s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') - (CASE WHEN DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(s_birth,'%m%d') THEN 0 ELSE 1 END)) AS age FROM student; -- 47、查询本周过生日的学生 SELECT * FROM student WHERE WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))=WEEK(s_birth) SELECT * FROM student WHERE YEARWEEK(s_birth)=YEARWEEK(DATE_FORMAT(NOW(),'%Y%m%d')) SELECT WEEK(DATE_FORMAT(NOW(),'%Y%m%d')) -- 48、查询下周过生日的学生 SELECT * FROM student WHERE WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =WEEK(s_birth) -- 49、查询本月过生日的学生 SELECT * FROM student WHERE MONTH(DATE_FORMAT(NOW(),'%Y%m%d')) =MONTH(s_birth) -- 50、查询下月过生日的学生 SELECT * FROM student WHERE MONTH(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =MONTH(s_birth)