文章目录
- 1、基本查询语句
- 2、在SQL PLUS中设置格式
- 3、查询表中的所有字段及指定字段
- 4、给字段设置别名(针对查询结果进行的,并不更改字段的名字)
- 5、运算符和表达式
- 6、在SELECT语句中使用运算符
- (1)使用算术运算符
- (2)使用比较运算符
- (3)使用逻辑运算符
- 7、带条件的查询
- (1)单一条件查询
- (2)多条件查询
- 8、模糊查询
- 使用LIKE查询
- 9、范围查询
- 10、对查询的结果排序
- 11、case…when 语句的使用
- 12、decode函数的使用
开始之前需要导入一些表,下载地址:https://yuyunyaohui.lanzous.com/ivPKcdnlecd 导入方法:
1、基本查询语句
SELECT [DISTINCT] column_name1,…|*
FROM table_name
[WHERE conditions]
2、在SQL PLUS中设置格式
COLUMN column_name HEADING new_name
//注:COLUMN 可以简写成 COL
col username heading 用户名;
select * from sys_user;
COLUMN column_name FORMATE dataformat
注:字符类型只能设置显示的长度
col username format a10;
select * from sys_user;
col salary format 9999.9;
select * from sys_user;
col salary format $9999.9;
select * from sys_user;
COLUMN column_name CLEAR
col username clear;
col salary clear;
select * from sys_user;
3、查询表中的所有字段及指定字段
col userid heading 用户编号;
col username heading 用户名;
col salary heading 工资;
select * from sys_user;
select username,salary from sys_user;
4、给字段设置别名(针对查询结果进行的,并不更改字段的名字)
SELECT column_name AS new_name,…
FROM table_name
注意:AS可以省略,用空格隔开原来的字段名和新字段名即可
select userid as 编号, username as 用户名,salary 工资
from sys_user;
select distinct username as 用户
from sys_user;
5、运算符和表达式
Oracle中的操作数可以有变量、常量和字段
算术运算符(+, -, *, /)
比较运算符(>, >=, <, <=, =, <> )
逻辑运算符( not, and, or )
6、在SELECT语句中使用运算符
(1)使用算术运算符
select userid, username,salary+200
from sys_user;
(2)使用比较运算符
select username from sys_user where salary < 900;
(3)使用逻辑运算符
select username
from sys_user
where salary > 800 and salary <> 990.8;
比较运算符的优先级高于逻辑运算符
7、带条件的查询
(1)单一条件查询
select salary
from sys_user
where username='李四';
select username,salary
from sys_user
where userid = 2;
(2)多条件查询
select * from sys_user
where username ='李四'
or salary > 1000;
8、模糊查询
LIKE
通配符的使用(_, %)
一个_只能代表一个字符
%可以代表0到多个字符
使用LIKE查询
查询用户名以“李”开头的用户信息
select * from sys_user where username like '李%';
查询用户名的第二个字符是“四”的用户信息
select * from sys_user where username like '_四%';
查询用户名中含有“小”的用户信息
select * from sys_user where username like '%小%';
9、范围查询
例:查询800到1000之间的员工工资
BETWEEN…AND
select * from sys_user where salary between 800 and 1000;
select * from sys_user where salary not between 800 and 1000;
IN/NOT IN
例:查询用户名是“张三”或者“李四”的用户信息
select * from sys_user where username in('张三','李四');
select * from sys_user where username not in('张三','李四');
10、对查询的结果排序
SELECT….FROM.…[WHERE…]
ORDER BY column1 DESC/ASC, …
select * from sys_user order by userid desc;
select * from sys_user order by username desc,salary asc;
11、case…when 语句的使用
CASE column_name
WHEN value1 THEN result1,…
[ELSE result] END
select username,case username when '张三' then 'sss部门'
when '李四' then 'xxx部门' else 'other部门' end as 部门
from sys_user;
CASE
WHEN column = value1
THEN result1,… [ELSE result] END
select username,case when username = '张三' then 'sss部门'
when username = '李四' then 'xxx部门' else 'other部门' end as 部门
from sys_user;
select username,case when salary < 900 then '工资低'
when salary > 1000 then '工资高' end as 工资水平
from sys_user;
12、decode函数的使用
decode(column_name, value1, result1,
…, defaultvalue)
select username, decode(username, '张三', 'sss部门', '李四', 'yyy部门', '其他')as 部门
from sys_user;