数据库select语句详解

SELECT

1.基本语法

  1. select * from 表名
    查询这张表所有内容。
  2. select 列名 from 表名
    查询这张表某一列所有内容。
  3. select 列名1,列名2…from 表名
    查询这张表的列1,列2,等多列。
  4. select distinct 列名 from 表名
    查询这一列去掉重复内容后的内容。
  5. select 表达式 from 表名
    查询表达式,下面会详细讲。
  6. select 列名(表达式)as 别名 from 表名
    给某一列或表达式取别名。

2.例子

如下这张表emp:
在这里插入图片描述
1)检索单个列
select ename from emp;
2) 检索多个列
select ename,job,sal from emp;
3) 检索所有列
select * from emp;
4) 去除重复
select distinct deptno from emp;
5) 别名
select ename as 姓名 from emp;
6) 伪列,即不存在的列,构建虚拟的列
select empno, 1*2 as count,‘cmj’ as
name,deptno from emp;
7)虚表,及不存在的表,可以计算
select 1+1 from dual;

3.where过滤器

a)、= 、 >、 <、 >=、 <=、 !=、 <>、 between and
b)、and 、or、 not、 union、 union all、
intersect 、minus
c)、null :is null、 is not null、 not is
null
d)、like :模糊查询 % _ escape(‘单个字符’)
e)、in 、 exists(难点) 及子查询

3.1 比较条件

= 、>、 <、 >=、 <=、 !=、 <>

  1. select * from emp where deptno = 20;
    即查询deptno为20的所有员工信息
  2. select * from emp where sal > 1500;
    查询员工工资大于1500的员工信息
  3. !=和<>都表示不等于

3.2 且或非

and、 or、 not

  1. select * from emp where sal>=1500 and sal<=3000;
    查询员工工资大于1500并且小于3000的员工信息
  2. select * from emp where sal<1500 or sal>3000;
    查询员工工资小于1500或大于3000的员工信息

3.3 null

null不能使用条件判断,只能使用is
–存在佣金的员工名称
select * from emp where comm is null;
–不存在佣金的员工名称
select * from emp where comm is not null;

3.4 集合操作

dept表
在这里插入图片描述

Union、Union All、Intersect、Minus
Union,并集(去重) 对两个结果集进行并集操作,不
包括重复行同时进行默认规则的排序;
Union All,全集(不去重) 对两个结果集进行并集操
作,包括重复行,不进行排序 ;
Intersect,交集(找出重复) 对两个结果集进行交集操
作,不包括重复行,同时进行默认规则的排序;
Minus,差集(减去重复) 对两个结果集进行差操作,不
包括重复行,同时进行默认规则的排序
–查询工资大于1500 或 含有佣金的人员姓名

  1. –union 去除重复行
    select ename from emp where sal>1500
    union
    select ename from emp where comm is not null;
  2. – union all 不去除重复行
    select ename from emp where sal>1500
    union all
    select ename from emp where comm is not null;
  3. –查询显示不存在雇员的所有部门号。
    select deptno from dept
    minus
    select distinct deptno from emp
  4. –查询工资大于1500 且 含有佣金的人员姓名
    select ename,sal,comm from emp where sal>1500
    intersect
    select ename,sal,comm from emp where comm is
    not null;

3.5 like:模糊查询

模糊查询,使用通配符:
%:零个及以上(任意个数的)的字符
_:一个字符
遇到内容中包含 % _ 使用escape(‘单个字符’)指定转义

  1. –查询员工姓名中包含字符A的员工信息
    select * from emp where ename like ‘%A%’;
  2. –查询员工姓名中包含第二个A的员工名称信息
    select * from emp where ename like ‘_A%’;
  3. –数据中 员工姓名中 存在 _ % ,如何查找:
    –1)、编写测试数据
    insert into emp(empno,ename,sal)
    values(1000,‘t_%test’,8989); insert into
    emp(empno,ename,sal)
    values(1200,‘t_tes%t’,8000);
    –2)、查找
    –查询员工姓名中包含字符%的员工名称 岗位 工资 部门编号
    select ename,job,sal,deptno from emp where
    ename like ‘%a%%’ escape(‘a’);
    –查询员工姓名中包含第二个_的员工名称 岗位 工资 部门编

3.6. in 与 exists

in相当于使用or的多个等值,定值集合 ,如果存在 子查
询,确保 类型相同、字段数为1,如果记录多,效率不
高,用于 一些 少量定值判断上
–10或30部门的雇员信息
select * from emp where sal in(900,800);
–子查询(查询中再有查询) in 只能存在一个字段
select * from emp where sal in (select sal
from emp e where deptno=10);

select * from emp where deptno in(10,30);
–部门名称为 SALES 或 ACCOUNTING 的雇员信息
select deptno from dept where dname
in(‘SALES’,‘ACCOUNTING’); SELECT *
FROM emp
WHERE deptno IN
(SELECT deptno FROM dept WHERE dname IN
(‘SALES’, ‘ACCOUNTING’));
exists条件为true,存在记录则返回结果,后续不再继续
比较查询,与查询的字段无关,与记录有关

3.7 排序

使用 ORDER BY 排序,排序不是真实改变存储结构的顺
序,而是获取的集合的顺序。
顺序 :asc(默认) desc
多字段: 在前面字段相等时,使用后面的字段排序
空排序: 降序为 desc,注意 null 为最后

  1. –按工资降序
    select * from emp order by sal desc;
  2. –null问题
    select * from emp order by nvl(comm,0),comm
    desc;
  3. –工资+佣金排序
    select ename,sal,comm,sal+nvl(comm,0) c from
    emp where deptno in(20,30) order by c;
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值