DQL(重点)

DQL(重点)

九、DQL(重点)
数据库在执行DQL的时候,不会对数据进行改动,仅仅是看。只是把服务器中的数据获取返回给客户端。
查询得到的结果 也是 一张表
9.1数据的准备
学生表:stu

雇员表:emp

部分表:dept

9.2基础查询
9.2.1 查询所有列:*
##(1,1)查询所有列:*表示所有列
##eg:查询所有的员工信息
select * from emp;
9.2.2 查询指定列
##(1,2)查询指定列
select empno,ename,deptno from emp;

9.3条件查询where
##(2)条件查询where

##(2,1)查询性别女,并且 年龄65的学生记录
select * from stu where gender=‘female’ and age = “65”;

9.3.1 常见的运算符
关系运算符:=、!=、<>、>=、<=
区间:betwen A and B --> 、[A,B]
And:并且,和。
Or:或者
Is null:空
Not:否,非
Is not null:非空
In:在什么里面
9.3.2或or、并且and

##(2,1)查询性别女,并且 年龄65的学生记录
select * from stu where gender=‘female’ and age = “65”;

##(2,2)查询学号是S_1001或者名字lisi的记录。
select * from stu where sid = ‘S_1001’ or age = ‘65’;
9.3.3在什么里面in
##(2,3)查询学号不是S_1001,S_1002,S_1003的记录
select * from stu where sid not in(‘S_1001’,‘S_1002’,‘S_1003’);
9.3.4是否为空is (not)null
##(2,4)查询年龄null的记录
select * from stu where age is null;

##(2,7)查询名字不为空的学生信息
select * from stu where sname is not null;
9.3.5 区间between
##(2,5)查询年龄为20~40岁之间的
方式一:select * from stu where age >= 20 and age <=40;
方式一:select * from stu where age between 20 and 40;
9.3.6非not
##(2,6)查询性别:非男的,学生记录
select * from stu where gender != ‘male’;
select * from stu where gender <> ‘male’;
select * from stu where not gender = ‘male’;
9.4模糊查询
9.4.1介绍
(1)当想查询学生姓名中包含字符a,就需要使用到模糊查询,模糊查询的话使用关键字是like(像)
(2)通配符
:任意一个字符
:张三,张三丰
张__:张三,张三丰
%:表示0或者多个字符串
张%
9.4.2 通配符:_
##查询名字有5个字母构成的 学生记录
select * from stu where sname like ‘_____’;

##查询名字由5个字母构成的,并且第5个字符是i 学生记录
select * from stu where sname like ‘____i’;
9.4.2通配符:%
##查询名字以‘z’开头的学生记录
select * from stu where sname like ‘z%’;

##查询名字中第2个字符是i的学生记录
select * from stu where sname like ’_i%‘;

##查询名字中包含a字符的学生记录
select * from stu where sname like ‘%a%’;

9.5字段的控制查询
9.5.1去除重复数据(distinct)
#9.5去除重复数据(distinct)
#(1)查询员工的所有部门
select distinct deptno from emp;
9.5.2 ifnull
##(2)查询员工的薪资(工资+奖金)。
##问题:任何问题 + null ->null
##1800 + null ->1800
ifnull(A,B):如果A是null,使用B的值,如果A不是null,就是使用A
select
sal,comm,sal + ifnull(comm,0)
from
emp;
9.5.3别名as
别名:给这个列 换 一个新的名字

##9,7别名
select
sal,comm,sal + ifnull(comm,0) as money
from
emp;

##as 关键字可以省略
select
ename name,sal,comm,sal + ifnull(comm,0)as money
from
emp;
9.5.4排序 order by asc/desc
升序:asc
降序:desc

##排序order by
##升序
##(1)查询所有的学生信息,根据年龄进行升序排序

select * from stu order by age asc;
##降序
##(2)查询所有的学生信息,根据年龄进行降序排列
select * from stu order by age desc;

##(2)查询所有的学生信息,根据名字进行升序排序
select * from stu order by sname asc;

##(3)查询所有员工信息,根据工资进行排序,如果工资相同按照员工升序
select * from emp order by sal desc,empno asc;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值