我们使用oracle自带的测试表进行查询学习,先使用建表语句创建数据表
dept表:
CREATE TABLE DEPT ( deptno INT NOT NULL PRIMARY KEY, dname VARCHAR(14), loc VARCHAR(13) );
emp表:
-- Create table CREATE TABLE EMP ( empno INT NOT NULL PRIMARY KEY, ename VARCHAR(10), job VARCHAR(9), mgr INT, hiredate DATE, sal DOUBLE, comm DOUBLE, deptno INT, CONSTRAINT FOREIGN KEY tb_emp_fk(deptno) REFERENCES DEPT(deptno) );
INSERT INTO DEPT (deptno, dname, loc) VALUES (10, 'ACCOUNTING', 'NEW YORK'); INSERT INTO DEPT (deptno, dname, loc) VALUES (20, 'RESEARCH', 'DALLAS'); INSERT INTO DEPT (deptno, dname, loc) VALUES (30, 'SALES', 'CHICAGO'); INSERT INTO DEPT (deptno, dname, loc) VALUES (40, 'OPERATIONS', 'BOSTON'); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7369, 'SMITH', 'CLERK', 7902,'1980-12-17', 899, NULL, 20); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7499, 'ALLEN', 'SALESMAN', 7698,'1981-02-20', 1633, 300, 30); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7521, 'WARD', 'SALESMAN', 7698,'1981-02-22', 1250, 500, 30); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7566, 'JONES', 'MANAGER', 7839,'1981-04-02', 2975, NULL, 20); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7654, 'MARTIN', 'SALESMAN', 7698,'1981-09-28', 1250, 1400, 30); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7698, 'BLAKE', 'MANAGER', 7839,'1981-05-01', 2850, NULL, 30); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7782, 'CLARK', 'MANAGER', 7839,'1981-06-09', 2450, NULL, 10); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, NULL, 20); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7839, 'KING', 'PRESIDENT', NULL,'1981-11-17', 5000, NULL, 10); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7876, 'ADAMS', 'CLERK', 7788,'1987-05-23', 1100, NULL, 20); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7900, 'JAMES', 'CLERK', 7698,'1981-12-03', 950, NULL, 30); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7902, 'FORD', 'ANALYST', 7566,'1981-12-03', 3000, NULL, 20); INSERT INTO EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7934, 'MILLER', 'CLERK', 7782,'1982-01-23', 1300, NULL, 10);
下面,我们进行查询操作:
1. 简单的select查询
# 最简单的select语句 select 字段,字段 从 表中 SELECT deptno, dname,loc FROM dept; SELECT * FROM dept; # *表示所有的列 SELECT deptno FROM dept; # 查询某一个字段的数据 SELECT * FROM emp;
2. 查询过程中的算数表达式
# 算数表达式 # 查询员工、月薪、年薪 SELECT ename,sal,sal*12 FROM emp;
3. 算数表达式的优先级
/** 乘法和除法的优先级高于加减法 同级运算从左到右 表达式中括号最优先 */ SELECT ename,sal,sal*(12+100) FROM emp;
4. 空值 null
/** 空值是指不可使用、未分配的值 空值不等于零或者空格 任何类型都可以支持空值 包括空值的任何算数表达式都等于空 字符串和null进行连接运算,得到的也是null */ SELECT * FROM emp WHERE comm IS NULL;
5. 查询时为列设置别名
/** 更改列的别名 */ SELECT ename '名字',sal '月薪',sal*12 '年薪' FROM emp;
6. 删除重复记录
/** 缺省情况下查询显示所有行,包括重复记录 */ SELECT deptno FROM emp; /** 可以使用DISTINCT清除重复记录 */ SELECT DISTINCT deptno FROM emp;
7. DISTINCT的作用范围是后面所有字段的组合
/** DISTINCT的作用范围是后面所有字段的组合 */ SELECT DISTINCT deptno,job FROM emp WHERE deptno=20;
8. where限定查询结果
/** 使用where可以限定返回的记录 */ SELECT * FROM emp WHERE deptno=20;
9. 字符串和日期要用单引号引起来
/** 字符串和日期要用单引号引起来 */ SELECT * FROM emp WHERE ename='SMITH'; SELECT * FROM emp WHERE hiredate='1980-12-17';
10. 比较运算符
SELECT ename,sal,comm,job FROM emp WHERE sal<=1500;
11. BETWEEN、 IN
/** 使用BETWEEN运算符查询某一段数据(包含最小值和最大值) */ SELECT ename,sal,comm,job FROM emp WHERE sal BETWEEN 1500 AND 3000; /** 使用IN运算符获得匹配列表值的记录 */ SELECT ename,sal,comm,job FROM emp WHERE mgr IN(7902,7566,7788); SELECT ename,sal,comm,job FROM emp WHERE mgr NOT IN(7902,7566,7788);
12. LIKE进行模糊查询
/** 使用LIKE运算符进行模糊查询 查询条件可包含中文或数字 (%) 可表示0或者多个字符 (_)可表示一个字符 */ SELECT ename,sal,comm,job FROM emp WHERE ename LIKE '%A%'; SELECT ename,sal,comm,job FROM emp WHERE ename LIKE '_L%';
13. AND OR
# 两个条件都满足 SELECT ename,sal,comm,job FROM emp WHERE sal>1000 AND job='SALESMAN'; # 两个条件满足一个 SELECT ename,sal,comm,job FROM emp WHERE sal>1000 OR job='SALESMAN';
14. 对结果集进行排序
#order by SELECT * FROM emp ORDER BY sal DESC; #从高到低 SELECT * FROM emp ORDER BY sal ASC; #从低到高