标签:
一、select 基本语法:
SELECT *|{[DISTINCT] column|expression [alias],...} FROM table;
SELECT 标识 选择哪些列。
FROM 标识从哪个表中选择。
1、从表中取出所有列
select * from emp;
Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 Connected as c##scott@ORCL SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- ----- ----------- --------- --------- ------ 7369 SMITH CLERK 7902 1980/12/17 800.00 20 7499 ALLEN SALESMAN 7698 1981/2/20 1600.00 300.00 30 7521 WARD SALESMAN 7698 1981/2/22 1250.00 500.00 30 7566 JONES MANAGER 7839 1981/4/2 2975.00 20 7654 MARTIN SALESMAN 7698 1981/9/28 1250.00 1400.00 30 7698 BLAKE MANAGER 7839 1981/5/1 2850.00 30 7782 CLARK MANAGER 7839 1981/6/9 2450.00 10 7788 SCOTT ANALYST 7566 1987/1/24 3000.00 20 7839 KING PRESIDENT 1981/11/17 5000.00 10 7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30 7876 ADAMS CLERK 7788 1987/4/2 1100.00 20 7900 JAMES CLERK 7698 1981/12/3 950.00 30 7902 FORD ANALYST 7566 1981/12/3 3000.00 20 7934 MILLER CLERK 7782 1982/1/23 1300.00 10 14 rows selected
2、从表中取出特定的列
SQL> SELECT ENAME,JOB FROM EMP; ENAME JOB ---------- --------- SMITH CLERK ALLEN SALESMAN WARD SALESMAN JONES MANAGER MARTIN SALESMAN BLAKE MANAGER CLARK MANAGER SCOTT ANALYST KING PRESIDENT TURNER SALESMAN ADAMS CLERK JAMES CLERK FORD ANALYST MILLER CLERK 14 rows selected
二、使用算术运算符
1、从EMP表中取出员工的年工资
SQL> SELECT ENAME,SAL*12,SAL*12+COMM FROM EMP; ENAME SAL*12 SAL*12+COMM ---------- ---------- ----------- SMITH 9600 ALLEN 19200 19500 WARD 15000 15500 JONES 35700 MARTIN 15000 16400 BLAKE 34200 CLARK 29400 SCOTT 36000 KING 60000 TURNER 18000 18000 ADAMS 13200 JAMES 11400 FORD 36000 MILLER 15600 14 rows selected
三、列的别名
SELECT column [AS] "别名" FROM table;
AS关键词可以省略 别名的双引号也可以省略 双引号主要是为了防止别名中含有空格或特殊符号
SQL> SELECT ENAME AS "姓 名",SAL 工资 FROM EMP; 姓 名 工资 ---------- --------- SMITH 800.00 ALLEN 1600.00 WARD 1250.00 JONES 2975.00 MARTIN 1250.00 BLAKE 2850.00 CLARK 2450.00 SCOTT 3000.00 KING 5000.00 TURNER 1500.00 ADAMS 1100.00 JAMES 950.00 FORD 3000.00 MILLER 1300.00 14 rows selected
四、使用连接符 || 合并列
SQL> SELECT ENAME || ‘的工作是‘ || JOB FROM EMP; ENAME||‘的工作是‘||JOB ------------------------------- SMITH的工作是CLERK ALLEN的工作是SALESMAN WARD的工作是SALESMAN JONES的工作是MANAGER MARTIN的工作是SALESMAN BLAKE的工作是MANAGER CLARK的工作是MANAGER SCOTT的工作是ANALYST KING的工作是PRESIDENT TURNER的工作是SALESMAN ADAMS的工作是CLERK JAMES的工作是CLERK FORD的工作是ANALYST MILLER的工作是CLERK 14 rows selected
五、去除重复行 DISTINCT
SQL> SELECT JOB FROM EMP; JOB --------- CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 14 rows selected SQL> SELECT DISTINCT JOB FROM EMP; JOB --------- CLERK SALESMAN PRESIDENT MANAGER ANALYST
标签:
原文地址:http://www.cnblogs.com/qunnel/p/5646433.html