2、限制和排序数据

2、限制和排序数据

限制语句:limiting the rows that are selected
1、限制行的查询使用 where 字句
2、where 字句放在 from 字句之后
3、条件放到 where 之后
select ename,job,sal,deptno from emp
where sal > 1000;
查询emp表中姓名、工作、工资、部门编号列,查看其中工资大于1000

字符和日期:character strings and dates
1、字符日期使用单引号引起来
2、字符区分大小写
3、注意日期格式、默认显示日期格式为:dd-mon-rr
select ename from emp
where hiredate = '17-feb-18'
查看2018.02.17日入职的人

比较操作符:comparison operators
= equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
<> not equal to
between ... and ...  介于两者之间的值
in (set)  匹配任何列表的值
like  匹配字符
is null 是一个空值

使用比较条件:using comparison operators
<=
select ename,sal from emp
where sal <= 3000;
查看工资小于3000的人
between
select ename,sal from emp
where sal between 2500 and 3000;
查看工资在2500和3000之间的人
in
使用in关系条件一般是被穷举的值
select ename,deptno from emp
where deptno in (10,20,30);
查看部门编号为10,20,30的人
like
使用like条件来执行通配符搜索有效的字符串的值
%表示0或多个字符,_表示一个字符
select ename from emp
where ename like 'S%';
查询以S开头的人
使用 escape 来转义%和_匹配字符本来的含义
select ename from emp
where ename like '%abc~%_%_' escape'~';
在~之后,%和_表示原来的名字中的含义

使用null条件
1、使用 is null 和 is not null 进行比较操作
select ename,mgr from emp where mgr is null ;
查找员工表的经理编号为空的人(king)

逻辑条件:defining conditions using the logical operators
操作符
and 两个条件都成立返回true 
or 任意一个成立返回true
not 如果条件不成立,返回true

select empno,ename,job,sal from emp
where sal >= 10000
and job id like '%MAN%';
全部符合条件的找出来
select empno,ename,job,sal from emp
where sal >=10000
or job id like '%MAN%';
符合上述一个条件即可
select ename,job from emp 
where job not in ('SYSMAN','MANGER');
查看工作不是分析员,不是经理的人

优先规则
1、算术运算符 +,-,*,/
2、连接符 ||
3、比较条件 >,<,>=,<=
4、is null ,like, in
5、between
6、not equal to <> ^= !=
7、not 逻辑条件
8、and 逻辑条件
9、or 逻辑条件

select ename,job,sal from emp
where job ='SYSMAN' 
          or job = 'SALEMAN'
          and sal > 10000;
逻辑条件and优先于or
(工作为销售和薪水大于10000的 and 运算)or  工作是分析员
结果中会出现薪水小于10000的人,他是分析员(或条件)

select ename,job,sal from emp
where (job = 'SYSMAN' or job= 'SALEMAN')
           and sal > 10000;
括号优先于所有
(工作为销售或者分析员) and  薪水大于10000

使用order by 字句:using the order by clause
1、排序检索行 order by 字句
asc: ascending order 升序排列,默认
desc:descending order 降序排列
order by 字句放在select 语句末尾

select ename,job,deptno,hiredate from emp
order by hiredate desc;
按照入职日期降序排序
select emp,ename,sal*12 annsal from emp 
order by annsal;
按照年薪升序排列
select ename,job,deptno,hiredate from emp
order by 4;
按照第四个(入职日期)升序排列
select ename,deptno,sal from emp
order by deptno desc,sal;
按照部门降序、薪水升序排列 (由于order by字句中部门编号在前,薪水在部门编号的基础上升序排序)

替代变量:substitution variables
1、临时变量使用&(每次都需要输入),调用代替变量使用&&(输入一次就可以了,仅当前会话有效)
2、替代变量可用于 where 字句,order by 字句,列表达式,表名,整个句子
使用变量前缀符号&可以提示用户输入值(图形化界面)
例如
SQL> select ename,deptno,empno,sal
  2  from emp
  3  where deptno=&departmentnumber;
Enter value for departmentnumber: 10   (用户提示输入)
old   3: where deptno=&departmentnumber
new   3: where deptno=10

ENAME           DEPTNO       EMPNO    SAL
---------- ---------- ---------- ----------
CLARK           10        7782       2450
KING           10        7839       5000
MILLER           10        7934       1300

字符和日期使用变量要用单引号引起来 
SQL> select ename,job,deptno    
  2  from emp
  3  where job = '&job';
Enter value for job: CLERK
old   3: where job = '&job'
new   3: where job = 'CLERK'

ENAME       JOB         DEPTNO
---------- --------- ----------
SMITH       CLERK         20
ADAMS       CLERK         20
JAMES       CLERK         30
MILLER       CLERK         10

列,表达式,文本
SQL> select ename,deptno,sal,'&job' from emp
  2  where &condition
  3  order by &column;
Enter value for job: SALESMAN
old   1: select ename,deptno,sal,'&job' from emp
new   1: select ename,deptno,sal,'SALESMAN' from emp
Enter value for condition: sal > 500
old   2: where &condition
new   2: where sal > 500
Enter value for column: 3
old   3: order by &column
new   3: order by 3

define命令:
定义变量的值,也可以取消undefine
SQL> define deptno=20
SQL> select ename,job,sal,deptno from emp
  2  where deptno=&deptno;
old   2: where deptno=&deptno
new   2: where deptno=20

ENAME       JOB            SAL     DEPTNO
---------- --------- ---------- ----------
SMITH       CLERK        800     20
JONES       MANAGER       2975     20
SCOTT       ANALYST       3000     20
ADAMS       CLERK       1100     20
FORD       ANALYST       3000     20

SQL> undefine deptno

veriyf命令:
变量替换过程新旧值是否显示
set verify on  显示变量替换过程
old   2: where deptno=&deptno
new   2: where deptno=20
set verify off 不显示此过程

猜你喜欢

转载自blog.csdn.net/weixin_43403578/article/details/89195889
今日推荐