学习环境配置:http://blog.chinaunix.net/uid-26230811-id-3330279.html
基本SELECT语句
- SELECT *|{[DISTINCT] column|expression [alias],...} FROM table;
- SQL> conn teach/oracle
- SQL> select * from departments;
- DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
- ------------- ------------------------------ ---------- -----------
- 10 Administration 200 1700
- 20 Marketing 201 1800
- 50 Shipping 124 1500
- 60 IT 103 1400
- 80 Sales 149 2500
- 90 Executive 100 1700
- 110 Accounting 205 1700
- 190 Contracting 1700
- 8 rows selected.
选择指定列
- SQL> SELECT department_id, location_id FROM departments;
- DEPARTMENT_ID LOCATION_ID
- ------------- -----------
- 10 1700
- 20 1800
- 50 1500
- 60 1400
- 80 2500
- 90 1700
- 110 1700
- 190 1700
- 8 rows selected.
编写SQL语句:
算术运算式
使用算术运算符
- SQL> SELECT last_name, salary, salary + 300 FROM employees;
- LAST_NAME SALARY SALARY+300
- ------------------------- ---------- ----------
- King 24000 24300
- Kochhar 17000 17300
- De Haan 17000 17300
- Hunold 9000 9300
- Ernst 6000 6300
- ……
运算符优先级
- 乘法和除法优先于加法和减法。
- 操作符相同的优先级从左向右计算。
- 括号用于强制优先求值和声明。
- SQL> SELECT last_name, salary, 12*salary+100 FROM employees;
- LAST_NAME SALARY 12*SALARY+100
- ------------------------- ---------- -------------
- King 24000 288100
- Kochhar 17000 204100
- De Haan 17000 204100
- Hunold 9000 108100
- Ernst 6000 72100
- Lorentz 4200 50500
- Mourgos 5800 69700
- ……
使用括号
SQL> SELECT last_name, salary, 12*(salary+100) FROM employees;
Defining a Null Value
SQL> SELECT last_name, job_id, salary, commission_pct FROM employees;
在算术表达式中的Null值
SQL> SELECT last_name, 12*salary*commission_pct FROM employees;
定义一个列别名
列别名:
- 重命名一个列标题
- 立即遵循列名称,也可以作为关键字之间的可选的列名称和别名
- 如果它包含空格或特殊字符或者是大小写敏感的需要双引号括起来
SQL> SELECT last_name AS name, commission_pct comm FROM employees;
SQL> SELECT last_name "Name", salary*12 "Annual Salary" FROM employees;
连接操作符
连接操作符:
- 由两个竖线(| |)连接
- 一个字符表达式创建一个合成列
SQL> SELECT last_name||job_id AS "Employees" FROM employees;
原义字符串
- 文字是一个字符,一个数字,或一个日期包含在选择列表。
- 日期和字符文字值必须包含在单引号。
SQL> SELECT last_name||' is a '||job_id AS "Employee Details" FROM employees;
重复行
默认显示查询的所有行,包括重复的行。
SQL> SELECT department_id FROM employees;
消除重复的行
SQL> SELECT DISTINCT department_id FROM employees;
阅读(3147) | 评论(0) | 转发(0) |