码迷,mamicode.com
首页 > 数据库 > 详细

oracle 开发 第2章 查询

时间:2016-01-05 16:54:37      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:

2016-01-05

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

oracle 开发 第2章 查询

标签:

原文地址:http://www.cnblogs.com/cenliang/p/5102884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!