oracle 开发 第02章 查询
2016-01-05 16:44
585 查看
2016-01-05
1.选择列
2.选择行
3.行标识符
4.行号
5.算术运算
6.日期运算
7.列运算
8.列别名
9.连接操作合并列
10.空值
11.去重复列
12.比较运算符
13.LIKE运算符
14.IN运算符
15.BETWEEN运算符
16.逻辑运算符
17.ORDER BY 排序
18.内连接
19.表别名
20.笛卡尔积
21.等值连接
22.不等连接
23.左外连接
24.右外连接
25.自连接
26.左外连接和自连接
27.内连接
28.内连接和不等连接
29.多表内连接
【参考资料】
[1] Jason Price.精通Oracle Database 12c SQL&PLSQL编程(第3版).[M].北京:清华大学出版社,2014
1.选择列
select customer_id,first_name,last_name,dob,phone from customers; select * from customers;
2.选择行
select * from customers where customer_id =2;
3.行标识符
select rowid,customer_id from customers;
4.行号
select rownum,customer_id,first_name,last_name from customers; select rownum,customer_id,first_name,last_name from customers where customer_id = 3;
5.算术运算
select 2*6 from dual; select 10*12/3-1 from dual; select 10*(12/3-1) from dual;
6.日期运算
select to_date('2012/7/25','yyyy/mm/dd') + 1 from dual; select * from dual; select to_date('2012/8/2','yyyy/mm/dd') - 3 from dual;
7.列运算
select name,price + 2 from products; select name,price*3 +1 from products;
8.列别名
select price*2 double_price from products; select price*2 "double price" from products; select 10*(12/3-1) as "computation" from dual;
9.连接操作合并列
select first_name||' '||last_name as "customers name" from customers;
10.空值
select * from customers; select customer_id,first_name,last_name,dob from customers where dob is null; select customer_id,first_name,last_name,phone from customers where phone is null; select customer_id,first_name,last_name,nvl(phone,'unkown phone number') as phone_number from customers; select customer_id,first_name,last_name,nvl(dob,to_date('2000/01/01')) as dob from customers;
11.去重复列
select customer_id from purchases; select distinct customer_id from purchases;
12.比较运算符
select * from customers where customer_id <> 2; select product_id,name from products where product_id < 8; select rownum,product_id,name from products where rownum <= 3; select * from customers where customer_id > any(2,3,4); select * from customers where customer_id > all(2,3,4);
13.LIKE运算符
select * from customers where first_name like '_o%'; --下划线匹配一个字符,百分号匹配任意多个字符 select * from customers where first_name not like '_o%'; select name from promotions where name like '%\%%' escape '\'; --\是转义字符,告诉数据库如何区分要搜索的字符和通配符
14.IN运算符
select * from customers where customer_id in (2,3,5); select * from customers where customer_id not in (2,3,5); select * from customers where customer_id not in (2,3,5,null); --列表中包含null值时,not in 不返回值
15.BETWEEN运算符
select * from customers where customer_id between 1 and 3; --区间是闭区间 select * from customers where customer_id not between 1 and 3; --not between检索2个开区间
16.逻辑运算符
select * from customers where dob > to_date('1970/01/01','yyyy/mm/dd') and customer_id > 3; select * from customers where dob > to_date('1970/01/01','yyyy/mm/dd') or customer_id > 3; select * from customers where dob > to_date('1970/01/01','yyyy/mm/dd') or customer_id < 2 and phone like '%1211'; --比较优于and,and优先于or select * from customers where dob > to_date('1970/01/01','yyyy/mm/dd') or (customer_id < 2 and phone like '%1211');
17.ORDER BY 排序
select * from customers order by last_name; --默认升序 select * from customers order by first_name asc,last_name desc; select customer_id,first_name,last_name from customers order by 1; --根据列的位置序号排序
18.内连接
select name,product_type_id from products where product_id = 3; select name from product_types where product_type_id = 2; select products.name,product_types.name from products,product_types where products.product_type_id = product_types.product_type_id and products.product_id = 3; --查询product_id为3的产品的名称及类型 select products.name,product_types.name from products,product_types where products.product_type_id = product_types.product_type_id order by products.name; --查询所有产品的名称及类型
19.表别名
select p.name,pt.name from products p,product_types pt where p.product_type_id = pt.product_type_id order by p.name;
20.笛卡尔积
select pt.product_type_id,p.product_id from product_types pt,products p;
21.等值连接
select c.first_name,c.last_name,p.name as prodcut,pt.name as type from customers c,purchases pr,products p,product_types pt where c.customer_id = pr.customer_id and p.product_id = pr.product_id and p.product_type_id = pt.product_type_id order by p.name; --查询顾客购买信息
22.不等连接
select * from salary_grades; --查询员工工资等级 select * from employees; --查询员工工资 select e.first_name,e.last_name,e.title,e.salary,sg.salary_grade_id from employees e,salary_grades sg where e.salary between sg.low_salary and sg.high_salary order by salary_grade_id; --查询员工的工资和工资等级
23.左外连接
select p.name,pt.name from products p,product_types pt where p.product_type_id = pt.product_type_id (+) order by p.name; select p.name,pt.name from products p left outer join product_types pt on p.product_type_id = pt.product_type_id order by p.name; --SQL/92标准 select p.name,pt.name from products p left outer join product_types pt using (product_type_id) order by p.name; --SQL/92标准
24.右外连接
select p.name,pt.name from products p,product_types pt where p.product_type_id (+) = pt.product_type_id order by p.name; select p.name,pt.name from products p right outer join product_types pt on p.product_type_id = pt.product_type_id order by p.name; --SQL/92标准 select p.name,pt.name from products p right outer join product_types pt using (product_type_id) order by p.name; --SQL/92标准
25.自连接
select * from employees; select w.first_name ||' '||w.last_name || 'works for '|| m.first_name ||' ' || m.last_name from employees w,employees m where w.manager_id = m.employee_id order by w.first_name; --查询每个雇员及其管理者的名字
26.左外连接和自连接
select w.last_name || 'works for '|| nvl(m.last_name,'the shareholders') from employees w,employees m where w.manager_id = m.employee_id (+) order by w.last_name; --查询每个雇员及其管理者名字,包括CEO
27.内连接
select p.name,pt.name from products p,product_types pt where p.product_type_id = pt.product_type_id order by p.name; --where子句,SQL/86标准 select p.name,pt.name from products p inner join product_types pt on p.product_type_id = pt.product_type_id order by p.name; --join子句,SQL/92标准 select p.name,pt.name from products p inner join product_types pt using (product_type_id) order by p.name; --using子句,等值连接,且连接列同名 select p.name,pt.name,product_type_id from products p inner join product_types pt using (product_type_id) order by p.name; --using子句引用列,不加表名或别名
28.内连接和不等连接
select e.first_name,e.last_name,e.title,e.salary,sg.salary_grade_id from employees e,salary_grades sg where e.salary between sg.low_salary and sg.high_salary order by salary_grade_id; select e.first_name,e.last_name,e.title,e.salary,sg.salary_grade_id from employees e inner join salary_grades sg on e.salary between sg.low_salary and sg.high_salary order by salary_grade_id;
29.多表内连接
select c.first_name,c.last_name,p.name as prodcut,pt.name as type from customers c,purchases pr,products p,product_types pt where c.customer_id = pr.customer_id and p.product_id = pr.product_id and p.product_type_id = pt.product_type_id order by p.name; --where子句,SQL/86标准 select c.first_name,c.last_name,p.name as prodcut,pt.name as type from customers c inner join purchases pr using (customer_id) inner join products p using (product_id) inner join product_types pt using (product_type_id) order by p.name; --join子句,SQL/92标准
【参考资料】
[1] Jason Price.精通Oracle Database 12c SQL&PLSQL编程(第3版).[M].北京:清华大学出版社,2014
相关文章推荐
- 批量move table\者重建索引
- ORACLE水位线讲解
- Oracle 12c: RMAN restore/recover pluggable database
- C#连接Oracle数据库的简单应用
- Oracle连接字符串总结
- oracle数据库表空间
- C# 连接 Oracle 的几种方式
- oracle 删除用户,提示“无法删除当前已连接的用户”
- oracle数据库之快速精简教程
- 如何提高oracle的查询速度(详解)
- oracle创建表空间
- 用mysql实现类似于oracle dblink的功能
- Windows C盘格式化或者同平台迁移oracle数据库
- oracle之使用dblink操作远程数据库
- Oracle11g 账号锁定及密码有效期问题
- oracle解除表锁定
- oracle修改序列
- 修改oracle用户密码永不过期
- oracle11R2 log_archive_format
- Oracle trunc()函数