1、DQL(数据查询语言)
- 所有得查询操作都用 select
- 简单得查询,复杂得查询他都能做
- 数据库最核心得语言,最重要得语句
- 使用频率最高得语句
2、查询指定字段
查询全部得学生
select *from student ;
查询指定字段
select id,name from student;
别名
select id as a,name as b from student as s;
– 函数 concat(a,b)
select concat('姓名:',name) as 新名字 from student;
有的时候,列名不是那么见名知意,可以起别名
去重 distinct
去除结果中的重复数据
select * from result
--查询全部得考试成绩
select distinct studentNo from result
--查询有哪些同学参加了考试
select version()
--查询系统版本(函数)
select 100*3-1 as 计算结果
--用来计算(表达式)
select @@auto_increment_increment
--查询自增的步长(变量)
select studentNo studentresult+1 as '提分后' from result
--学员考试成绩 +1分查看
数据库中的表达式
文本值、列、NULL、函数、计算表达式、系统变量
select 表达式 from 表
where条件子句
作用:检索数据中符合条件的值
逻辑运算符
运算符 | 语法 | 描述 |
---|---|---|
and && | a and b a&&b | 逻辑与,两个都为真,结果为真 |
or | 逻辑或,其中一个为真,则结果为真 | |
Not ! | 逻辑非 |
模糊查询:比较运算符
运算符 | 语法 | 描述 |
---|---|---|
IS NULL | a is null | 如果操作符为NULL,结果为真 |
IS NOT NULL | ||
between | ||
like | 经常结合%(多个字符) _(一个字符) | |
in | a in (a1,a2,a3…) | 一个 多个 具体的值 |
多练习
联表查询
七种join
select s.studentNO,studentName,SubjectNO,StudentResult
from student as s
innter join result as r
on s.studentNO=r.studentNO;
select s.studentNO,studentName,SubjectNO,StudentResult
from student s
right join result r
on s.studentNO=r.studentNO;
select s.studentNO,studentName,SubjectNO,StudentResult
from student s
left join result r
on s.studentNO=r.studentNO;
select s.studentNO,studentName,SubjectNO,StudentResult
from student s
left join result r
on s.studentNO=r.studentNO
where StudentResult is null;
select s.studentNO,studentName,SubjectNO,StudentResult
from student s
right join result r
on s.studentNO=r.studentNO
inner join subject sub
on r.subjectNo = sub.subjectNo;
自连接
自己的表和自己的表连接,一张表拆为两张一样的表
比如:
categoryid | pid | categoryName |
---|---|---|
3 | 1 | 软件开发 |
5 | 1 | 美术设计 |
4 | 3 | 数据库 |
8 | 2 | 办公信息 |
2 | 1 | 信息技术 |
6 | 3 | web开发 |
7 | 5 | ps技术 |
父类
categoryid | categoryName |
---|---|
2 | 信息技术 |
3 | 软件开发 |
5 | 美术设计 |
子类 | |
pid | categoryid |
– | – |
3 | 4 |
2 | 8 |
3 | 6 |
5 | 7 |
操作:查询父类对应的子类关系
父类 | 子类 |
---|---|
信息技术 | 办公信息 |
软件开发 | 数据库 |
软件开发 | web开发 |
把一张表拆开
select a.categoryName as '父栏目',b.categoryName as '子栏目'
from category as a , category as b
where a.categotyid = b.pid
分页和排序
分页,每页只显示五条数据,语法:limit 当前页,页面的大小
limit 0,5: 1-5页
第一页:limit 0,5 (1-1)* 5
第二页:limit 1,5 (2-1) * 5
第N页:limit 0,5 (n-1) * pageSize,pageSize
【pageSize:页面大小】
【(n-1)*pagesize:起始值】
【n:当前页】
【数据总数/页面大小 = 总页数】
select s.studentNO,studentName,subjectName,studentResult
from student s
inner join result r
on s.studentNO = r.studentNO
inner join subject sub
on r.subjectNO = subjectNO
where subjectName = '数据库结构-1'
order by studentResult ASC
limit 1,5
—查询Java第一学年 课程成绩排名前十的学生,并且分数要大于80的学生信息(学号,姓名,课程名称,分数)
select s.studentNO,studentName,studentResult
from student s
inner join result r
on s.studentNO=r.studentNO
where subjectName='Java第一学年' and studentResult>=80
order by studentResult desc
子查询
where (这个值是计算出来的)
本质:在where语句中嵌套一个子查询语句
由里及外依次执行。
常用函数
– 数学运算 –
select abs(-8) ---绝对值
select ceiling(9.4) ---向上取整
select floor(9.4) ---向下取整
select rand() ---返回一个0~1的随机数
select sign(-10) ---判断一个数的符号 0-0 负数返回 -1,正数返回 1
–字符串函数–
select char_length('技术是搞起来的') --字符长度
select concat('我','爱','阿','土') --拼接字符串
select insert('我爱编程helloworld',1,2,'超级热爱') --(将我爱改成超级)查询,替换
select lower('wanaei') --小写字母
select upper('wanaei') --大写字母
select instr('wanaei','a') --返回第一次出现的子串的索引
select replace('阿土伯伯说你就是最棒的','你','我') --替换出现的指定字符串
select substr('阿土伯伯说 你能替换我?',4,6) --返回指定的子字符串
select reverse('hhhhhhhyiiyy') --反转
–查询姓周的同学,名字 邹
select replace(studentname,'周','邹') from student
where studentname like '周%'
–时间和日期函数(记住)
select current date() --获取当前日期
select curdate() --获取当前日期
select now() --获取当前的时间
select localtime() --本地时间
select sysdate() --系统时间
select year(now()) --
select month(now()) --
select day(now()) --
select minute(now()) --
select second(now()) --
--系统
select system_user()
select user()
select version()
聚合函数(常用)
函数名称 | 描述 |
---|---|
count | |
sum | |
avg | |
max() | |
min |
select count(studentname) from student; -- count(字段名),会忽略所有的null值
select count(*) from student; --count(*),不会忽略null值 本质计算行数
select count(1) from result; --会忽略所有的null值
--cha'xun
select subjectName, avg(studentResult) as 平均分, max(SutdentResult)
from result r
inner join 'subject' sub
on r.subjectNO = subjectNO
group by r.subjectNO
having 平均分>80