Oracle超详细学习笔记-----员⼯培训----
--1.最简单的查询
--例
SELECT *
FROM employees;
DESC employees;
SET linesize 600;
SET PAGESIZE 50;
--例2
SELECT table_name FROM user_tables;--查询数据库中所有表名
--2.查询特定的列
-
-例
SELECT employee_id,fisrt_name,last_name.salary
FROM employees;
--3.在SELECT⼦句中使⽤直接量
--例
SELECT 5
FROM employees;
-----------------------
SELECT 5
FROM dual;--这是Oracle提供的伪表
--例2:
SELECT 'Hello Oracle SQL'--在Oracle中如何表⽰字符串常量
FROM employees;
SELECT 'Hello Oracle SQL'--在Oracle中如何表⽰字符串常量
FROM dual;
--例3:
SELECT 'Hello Oracle''s SQL'--⽤两个连续单引号进⾏单引号转义
FROM employees;
--4在SELECT⼦句中使⽤算术表达式
--例1:查询员⼯的编号、姓名和年薪
SELECT employee_id,fist_name,last_name,salry,salary*12
FROM employees;
-
-例2:
SELECT 5/2
FROM dual;
--5.给例定义别名
--例1:
SELECT employee_id AS id
FROM employees;
oracle trunc函数的使用方法--例2:
SELECT employee_id AS "Id"--双引号中的字符原样显⽰
FROM employees;
--例3:
SELECT employee_id,fist_name,last_name,salry,salary*12 AS annual_salary FROM employees;
--例4:
SELECT employee_id,fist_name,last_name,salry,salary*12 AS "annual salary" FROM employees;
--如果别名有特殊字符必须使⽤双引号
--6.字符串的链接运算符
--例:
SELECT employee_id||''||fist_name,last_name,salry,salary*12 AS annual_salary FROM employees;
--7.过滤重复记录
--例1:
SELECT DISTINCT department_id
FROM employees;
--例2:
SELECT DISTINCT department_id,job_id
FROM employees;
--第⼆章过滤查询和结果集排序
--1.使⽤WHERE⼦句过滤记录
--例1:
SELECT *
FROM employees
WHERE salary > 8000;
-
-例2:
SELECT employee_id,first_name,last_name,salary FROM employees
WHERE salary = 17000;
--例3:
SELECT employee_id,first_name,last_name,salary FROM employees
WHERE salary <> 17000;-- !=
--⽐较运算符号:>,>=,<,<=,=,!=或<>
--例4
SELECT employee_id,first_name,last_name,salary FROM employees
WHERE salary >=2500 AND salary <= 17000;
--逻辑运算符:AND,OR,NOT
-
-2.在WHERE⼦句中使⽤字符串和⽇期
--例1:查询职位ID为st_clerk的员⼯信息
SELECT employee_id,job_id,first_name,
FROM employees
WHERE job_id = 'ST_CLERK';
--表中字段的值是区分⼤⼩写的
--例2:查询1998年7⽉9⽇⼊职的员⼯信息
SELECT employee_id,job_id,first_name
FROM employees
WHERE hire_date = '09-7⽉-1998';
--⽇期直接量必须以字符串的形式来体现
-
-Oracle的⽇期是格式敏感的
--标准⽇期表⽰DD-MM-RR
--3.其他⽐较运算符
--1)BETWEEN AND
--例1:查询⼯资在【2500,7000】上的员⼯信息
SELECT employee_id,job_id,first_name,salary
FROM employees
WHERE salary BETWEEN 2500 AND 7000;
--包含边界值
--第⼀个值要⼩于第⼆个值
--练习:查询1998年⼊职的员⼯信息
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE hire_date BETWEEN '01-1⽉-1998' AND '31-12⽉-1998';
--2) IN
--例1:查询职位ID是ST_CLERK或者ST_MAN的员⼯
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE job_id IN ('ST_CLERK','ST_MAN');
--例2
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE NOT job_id IN ('ST_CLERK','ST_MAN');
----------------------------------------------------------------------------
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE job_id NOT IN ('ST_CLERK','ST_MAN');
--3)LIKE
--例1:模糊查询
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE job_id LIKE 'ST%';
--通配符:%任意多个字符,_⼀个字符。
-
-只能⽤于⽇期或者字符串
--练习:
--查询first_name⾸字母是⼤写S,第三个字母是e的员⼯
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE first_name LIKE 'S_e%';
--查询first_name含有字母a和e的员⼯
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE first_name LIKE '%a%e%' OR first_name LIKE '%e%a%';
--查询1988年⼊职的员⼯
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE hire_date LIKE '%98';
--例2:查询职位ID含有_P的员⼯信息
SELECT employee_id,job_id,first_name,salary,hire_date
FROM employees
WHERE job_id LIKE '%\_P%' ESCAPE '\';
--转义字符是开发者⾃⼰定义的
--4.SELECT,FROM和WHERE⼦句的执⾏顺序 ```
--例:
SELECT employee_id,last_name AS ename,salary
FROM employees
WHERE last_name = 'King'; --Error,在WHERE⼦句中不能使⽤列的别名 --执⾏顺序:FROM--------------->WHERE------------->SELECT
--5).结果集排序
--例:
SELECT employee_id,first_name,last_name ,salaty
FROM employees
ORDER BY salary DESC;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论