查询语句 select [单表操作]

---SQL单表简单查询
1、单表操作&别名的使用

1  --查询表的所有数据:select * from 表名;通配符*代表所有
2  select * from emp;

1 --查询表中指定字段的值:select 字段名1,字段名2,... from 表名;
2  select empno,ename,job,sal,hiredate,comm from emp;

1 --查询结果中的字段使用别名:作用[方便查看查询结果]
2      --在字段名后使用关键字 字段名 as "别名"
3      --注意:as关键字可以缺省不写,别名中没有特殊的字符双引号也可以缺省
4  --方式-1
5  select empno 员工编号 from empno;
6  --方式-2
7  select empno "员工编号",ename "员工姓名" from emp;
8  --方式-3
9  select empno as "员工编号",ename as "员工姓名",job as "工作职位" from emp;


2、单表查询连接符 & 去除重复 & 排序 & 字段逻辑运算

1  --连接符 SQL语句中的连接符 || ,使用在select 和 from 之间,拼接好的连接在结果集中作为一个新的字段显示,可使用别名优化字段显示。
2      --select '字符-1'|| 字段名-1||'字符-2'|| 字段名-2 from 表名;
3  select '职工编号为:'|| empno || ',的名字是'|| ename as "信息表" from emp;

1  -- 去除重复 distinct [规则:按行去除,多行数据完全相同取其一]
2  select distinct job from emp;--[工种]
3  select distinct job,mgr from emp;

1 --排序 order by [默认升序:asc(可省略不写),降序:desc]
2  --单字段排序
3  select * from emp order by empno desc;--[按照职工编号降序]
4  select ename,job,sal,comm from emp order by sal desc;
5  --多字段排序
6  --select * from 表名 order by 字段名1 ,字段名2...;
7  --[先按字段名1进行排序,字段名1相同的在安字段名2进行排序...]
8  select * from emp order by empno,ename;
1  --字段的逻辑运算
2  select ename,job,comm,sal,sal+1000 as "所有人的薪水+1000",sal + comm "薪水+奖金" from emp;


3、单表查询&单条件where子句
--select 字段名,字段名,...from表名 where 筛选条件
--单筛选条件
--使用运算符进行筛选 =,>,>=,<,<=,<> 单个条件中
--注意:如果条件中的值为字符,必须使用单引号括起来

 1 --查询所有的员工的工资信息
 2 select empno,ename,sal+comm as 薪资 from emp
 3 --查询SMITH的个人信息
 4 select * from emp where ename='SMITH'
 5 --查询SMITH的薪资信息,逻辑运算符=
 6 select empno,ename,sal,sal+comm from emp where ename='SMITH'
 7 --查询工资大于1000的员工信息,逻辑符>
 8 select * from emp where sal>'2000'
 9 --查询工资不等于3000的员工信息
10 select * from emp where sal<>3000 order  by sal

小练习-1

 1 --查看工资等于1250的员工信息
 2 select *from emp where sal='1250'
 3 --查看工作等于CLERK的员工信息
 4 select * from emp where job='CLERK'
 5 --查看工资大于1250的员工姓名和工作
 6 select ename,job from emp where sal>1250
 7 --查看工资大于等于2000的员工信息
 8 select * from emp where sal>=2000;
 9 --查看工资小于等于2000的员工信息;
10 select * from emp where sal<=2000;
11 --查看工资不等于1500的员工信息
12 select * from emp where sal<>1500;
13 --查看入职日期在81年后的员工信息
14 --注意:oracle默认的日期格式为 日-月-年,示例'03-1月-1981'
15 select * from emp order by hiredate
16 select * from emp where hiredate>='01-1月-1981' order by hiredate
View Code

4、where子句关键字
--多条件筛选(where子句关键字:and,or,like,is null,is not,in,betwwen and )

1 --查询工资在2000-3000之间的员工信息
2  --使用and关键字,多条件同时成立
3 select * from emp where sal>=2000 and sal<3000;
4  --使用between and 【闭区间】
5 select * from emp where sal between 2000 and 3000;
1 --查询工作为SALESMAN,ANALYST,MANAGER的员工信息
2  --使用or 关键字进行或条件的筛选查询
3 select * from emp where job='SALESMAN' or job='ANALYST' or job='MANAGER' order by job;
4 --使用in关键字也可以进行筛选,但in中的内容只能为一个字段
5 select * from emp where job in('SALESMAN','ANALYST','MANAGER');

模糊查询:like
--查询姓名中包含S的,以S开头的,S结尾的,第二个字符A的

 1 -- % 号表示任意多个的任意字符
 2 --select * from 表名 where 字段名 like '%字符%':查询包含指定字符的数据
 3 select * from emp where ename like '%S%';--包含S
 4 
 5 --select * from 表名 where 字段名 like '字符%':查询以指定字符开头的数据
 6 select * from emp where ename like 'S%';--以S开头
 7 
 8 --select * from 表名 where 字段名 like '%字符':查询包含指定字符结尾的数据
 9 select * from emp where ename like '%S';--以S结尾
10 
11 --select * from 表名 where 字段名 like '_字符%':查询指定位置为指定字符的数据
12  -- _表示一个任意字符
13 select * from emp where ename like '_A%';--第二个字符A的

特殊情况:escape:

select * from emp where ename like '%\_%' escape '\';--名字包含_
1 --查询有奖金的员工信息
2  --is null 字段值为null
3  --select * from 表名 where 字段名 is null;=>字段值为null
4  --select * from 表名 where 字段名 is not null;=>字段值不为null
5  --多个条件使用and关键字进行连接
6 select * from emp where comm is not null and comm>0;

猜你喜欢

转载自www.cnblogs.com/cao-yin/p/9726223.html