查询_子查询_join
查询query
完整的select语句
select [distinct] [all] column,group_function from table
[where condition]
[group by group_by_expression]
[having group_condition]
[order by column];
各个子句的特性
select子句
格式:select [distinct] [all] [表名|表别名].列 ["别名"|别名,]... 列:表的列 或 表达式。
若别名不含空格,尽量少用这种格式 : "别名"。因为,双引号中的字符串,Oracle是区分大小写的。
在Oracle一个列的完整标识 :[schema.][table.]column
from子句
格式:from 表名 [别名]
若定义了表别名,则在整个select语句中,只能用表别名来标识表。
where子句
格式:where condition
condition为查询的限制条件;
where子句中不能使用“组函数”;
condition中可包含子查询。
group by子句
格式:group by group_by_expression group by 子句,对所有选定的记录,做“分组”操作。
若select语句中有group by子句,则select子句中的所有列,要么作为“组函数”的参数,要么都写到group_by_expression中;
group_by_expression使用的列,可以不出现在select子句中,但一定是表中的列。
having子句
格式:having group_condition having子句,对所有组做“筛选”,选出符合条件的分组。
group_condition中可包含子循环。
order by子句
格式:order by column
对查询得出的记录,进行排序:升序:asc 或 降序:desc
子查询subquery
子查询分为:单行子查询和多行子查询
单行子查询
select ….
from ….
where ….=
(
子查询
)
多行子查询
select ….
from ….
where ….in
(
子查询
)
注意:
1, 子查询不能包含order by 子句.
2, 若明确指定子查询返回的行数为一,不用in 而用 = 接收返回的信息,提高性能.
3, 子查询不能访问外查询的数据(变量,列),否则查询不能执行或返回不正确的操作
结果.
4—子查询可用在select语句的多处 : select子句中,from子句中,where子句中,having子
句中等.
多表查询
重要知识点
, 编写多表查询的select语句
, outer join
, self join
join
在多个表中查询数据;
rows are joined using common values,typically primary and foreign key values;
join是一个动作。
join的类型
join分为两类:
join
equijoin : 使用等号(=)来join
non-equijoin : 不使用等号来join
outer join : view data that would not normally meet a join condition
self join :
self join : 一个表,连接自身的副本。
set operators : 通过运算来构造join condition。
join的个数,表的个数 , 1
笛卡儿积(Cartesain Product) select e.id,e.name,d.name
from employee e,department d
A Cartesian product is formed when
-- A join condition is omitted(忽略).
-- A join condition is invalid(无效).
-- All rowd in the first table are joined to all rows in the seconde table.
为了避免产生笛卡儿积,join应该加where子句.
outer join
左外连接 : 把右表的所有记录全部显示
右外连接 : 把左表的所有记录全部显示
完全外连接 : 把左表和右表的记录都显示出来
左外连接
select table.column,table.column from table1,table2
where table1.column(+) = table2.column 实例:
column d.dept_name format a20
column e.last_name format a20
select e.id,e.last_name,d.dept_name
from emp_wzh e,dept_wzh d
where e.dept_id(+) = d.id
右外连接
select table.column,table.column from table1,table2
where table1.column = table2.column(+)
实例:
column d.dept_name format a20
column e.last_name format a20
select e.id,e.last_name,d.dept_name
from emp_wzh e,dept_wzh d
where e.dept_id = d.id(+)
完全外连接
左外连接
union | union all | intersect | minus 右外连接
union : 两个集合的全部记录,但重复的只显示一次
union all : 两个集合的全部记录
intersect : 两个集合的交集(公共的记录)
minus : 两个集合的差(左外连接 – 右外连接 = 不相同的记录) 实例:
column d.dept_name format a20 column e.last_name format a20 select e.id,e.last_name,d.dept_name from emp_wzh e,dept_wzh d
where e.dept_id(+) = d.id
union
select e.id,e.last_name,d.dept_name from emp_wzh e,dept_wzh d
where e.dept_id = d.id(+)
equijoin
select e.id,e.last_name,d.dept_name from emp_wzh e,dept_wzh d
where e.dept_id = d.id
non-equijoin
select e.id,e.last_name,e.dept_id,d.id,d.dept_name
from emp_wzh e,dept_wzh d
where e.dept_id < d.id -- 可使用其它运算符
self join
select worker.last_name||’ work for ’||manager.last_name from s_emp worker,s_emp manager where worker.manager_id = manager.id
Create table
OracleSQL可创建三种表
relational table
object table
XMLtype table
创建relational table
CREATE [GLOBAL TEMPORARY] TABLE [schema.]table (
列定义,
表定义
)
列定义
column datatype [DEFAULT expr] [constraint 约束名 约束类型] 表定义
… | 表约束
约束
out_of_line_constraint 分为inline_constraint 和
not null约束一定为inline_constraint,其他约束可为inline_constraint
或 out_of_line_constraint.
列层面上的约束
constraint constraint_name [ primary key | not null | unique
| check(column in(…) )
| references 父表(主键) ]
表层面上的约束
constraint constraint_name [ primary key (columns)
| unique (columns)
| check(column in(…) )
| foreign key (columns) references 父表(主键) ]
利用subquery创建表
CREATE [GLOBAL TEMPORARY] TABLE [schema.]table as
subquery
上述方式创建的表,即可是有数据的表,也可是空表.
构造空表
select columns
from table
where 逻辑错误(true=false)
end
本文档为【查询_子查询_join】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。