红色标注为测试未通过,绿色需要重新备注
1.mysql 数据库的常用指令
*代表所有列
查询表内容
selecet * from stu; 查看stu表中的所有列
修改表内容
Update teacher stu sname “李军”=”李三” 修改teacher表姓名为李军改为李三
删除:
Delete from 表名; 删除 从 哪个表
查询条件
(1)简单查询
select * from Info 查询lnfo表中的所有列
备注 ... as ....
例:select Code as '代号',Name as '姓名' from Info
把lnfo表中code备注为代号,name备注为姓名
(2) 条件查询
Where后面跟条件 条件要写清楚 或者用or 并且用and
示例
1.查询成绩表中成绩(degree)为92的信息
Select * from score where degree =”92”;
2.查询成绩表中课程号是3-245并且成绩是86的信息
Select * from score where cno='3-245' and degree=”86”;
3.查询成绩表中课程号是3-245并且成绩是86的信息
Select * from score where cno='3-245' or degree=”86”;
(3) 模糊查询 like not like
示例
查找老师表中姓李的 名字为两个字的信息
select * from teacher where name like '李%';
或者为 select * from teacher where name like "李_ _";
%代表任意多个字符 _代表一个字符
(4)排序查询 order by 字段 排序值(desc/asc )(降序/升序)升序为系统默认可不备注
以Class降序查询Student表的所有记录
select * from student order by class desc;
(5)范围查询 关系运算符 <, >, >=, <=, !=, <>不等于
between......and...... ,在...跟....之间
查询Score表中成绩在60到80之间的所有记录
select * from score where degree between 60 and 80;
也可表达为 select * from score where degree >60 and degree<80;
6)离散查询 in not in
select * from student where sname in ('张三','李四');
student表中sname为张三,李四,的信息
(7)聚合函数,统计查询
select sum(Price) from Car
#查询所有价格之和 sum()求和
select count(Code) from Car
#查询数据条数
select max(Code) from Car
#求最大值
select min(Brand) from Car
#求最小值
select avg(Price) from Car
#求平均值
在一个表中显示成绩总和条数最大值最小值平均数:
Select sum(degree) as”总和”,
Count(degree) as”条数”,
Max(degree) as”最大”,
Min(degree) as”最低”,
avg(degree) as”平均”from score;
(8)分页查询 limit 从第几条开始,取多少条数据
#每页显示5条数据,取第2页的数据
select * from student limit (pageSize-1)*5,5
(9)去重查询distinct
select distinct cno from score;
从score表中去重cno列下内容
(10)分组查询 group by 字段 having 条件
select count(*),cno,group_concat(degree),sum(degree) from score group by cno ;
select cno,group_concat(degree),sum(degree) from score group by cno having count(*)>3
#分组之后根据条件查询使用having 不使用where
高级查询
连接查询,对列的扩展
Select * from student as stu,score as sc
where stu.sno = sc.sno and sc.sno = “103” ;
2.联合查询,对行的扩展
select Code,Name from Info union
select Code,Name from Nation
3.子查询
(1)无关子查询
外层查询 (里层查询)
子查询的结果当做父查询的条件
子查询:select Code from Nation where Name='汉族'
父查询:select * from Info where Nation = ''
select * from Info where Nation = (select Code from Nation where Name='汉族')
(2)相关子查询
查询汽车表中油耗低于该系列平均油耗的所有汽车信息
父查询:select * from Car where Oil<(该系列平均油耗)
子查询:select avg(Oil) from Car where Brand = '某个系列'
select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand )