文章目录
SQL语句的使用
select语句的基本使用
select * from Employees;
select * from employees where EMPLOYEE_ID='100';
1. select语句中的算数表达式
+, -, *, /: 可用算数表达对数字和日期进行运算
注意, 计算是在查询出结果之后再对结果进行计算, 并不会对表中的数据进行该表
对日期进行操作的时候, 我们只能对DATE和TIMESTAMP数据类型进行操作
实例一: 计算目前距离employee_id为100的员工入职时间有多少天
SQL> select (sysdate - hire_date) || '天' from employees where employee_id=100;
(SYSDATE-HIRE_DATE)||'天'
-------------------------------------------
5885.44689814814814814814814814814814815天
实例二: 计算所有员工全年薪水加100 之后的结果
SQL> select employee_id, first_name, (salary * 12) + 100 from employees;
EMPLOYEE_ID FIRST_NAME (SALARY*12)+100
----------- -------------------- ---------------
100 Steven 288100
101 Neena 204100
102 Lex 204100
103 Alexander 108100
示例三: 计算所有员工薪水加100之后的全年薪水
SQL> select employee_id, first_name, (salary + 100) * 12 from employees;
EMPLOYEE_ID FIRST_NAME (SALARY+100)*12
----------- -------------------- ---------------
100 Steven 289200
101 Neena 205200
102 Lex 205200
103 Alexander 109200
2. 定义空值
空值是一个未分配的, 未知的, 或不适用的值, 空值既不是0, 也不是空格
- 如果一行中缺少某个数据, 我们可以把它列为空值
- 如果算数表达式中的算子有空值, 那么结果一定为空值
实例一: 查看employee_id为100的员工commission_pct是多少
SQL> select employee_id,last_name, commission_pct from employees where employee_id = 100;
EMPLOYEE_ID LAST_NAME COMMISSION_PCT
----------- ------------------------- --------------
100 King
实例二: 对employee_id为100的员工佣金进行计算
SQL> select employee_id,last_name, commission_pct * 12 from employees where employee_id = 100;
EMPLOYEE_ID LAST_NAME COMMISSION_PCT*12
----------- ------------------------- -----------------
100 King
可以看到, 并没有数值
3. 定义别名
别名就是标题列的别名, 使用AS关键字, 如果别名中包含特殊字符, 则需要使用("")将别名括起来
AS关键字可以不写, 默认使用空格就行
实例:
SQL> select last_name, salary as 工资 from employees where employee_id=100;
LAST_NAME 工资
------------------------- ----------
King 24000.00
SQL> select last_name, salary 工资 from employees where employee_id=100;
LAST_NAME 工资
------------------------- ----------
King 24000.00
4. 连字运算符
连字运算就是两个字符串连接, 也可以和算数运算结果或表达式进行连接
关键字为||
实例一: first_name和last_name进行连接
SQL> select first_name || last_name from employees where employee_id=100;
FIRST_NAME||LAST_NAME
---------------------------------------------
StevenKing
实例二: 连接字符串常量
SQL> select first_name || ', '|| last_name from employees where employee_id=100;
FIRST_NAME||','||LAST_NAME
-----------------------------------------------
Steven, King
实例三: 连接表达式
SQL> select first_name || ', '|| last_name || '年薪为:' || salary * 12 from employees where employee_id=100;
FIRST_NAME||','||LAST_NAME||'?
--------------------------------------------------------------------------------
Steven, King年薪为:288000
5. 文字字符串
文字字符串是一个用单引号引起来的字符串, 常用于表示一个文字字符串. 与双引号不同的是, 双引号常用于表示别名
实例: 显示所有雇员的名字和工作代码, 使用is a 连接查询结果。列标题用Employee Details。
SQL> select last_name || ' is a ' || job_id as "Employee Details" from employees;
Employee Details
-----------------------------------------
Abel is a SA_REP
Ande is a SA_REP
Atkinson is a ST_CLERK
Austin is a IT_PROG
Baer is a PR_REP
Baida is a PU_CLERK
Banda is a SA_REP
Bates is a SA_REP
6. 去除重复数据
去除重复数据关键字是distinct, 他会去除查询中重复的数据
实例: 查询员工表中所有的deparment_id
SQL> select department_id from employees;
DEPARTMENT_ID
-------------
...
...
70
110
110
107 rows selected
去除关键字之后的数据
SQL> select distinct department_id from employees;
DEPARTMENT_ID
-------------
100
30
90
20
70
110
50
80
40
60
10
12 rows selected