设为首页 加入收藏

TOP

数据库中表的复杂查询
2015-11-21 01:29:59 来源: 作者: 【 】 浏览:0
Tags:数据库 中表 复杂 查询
数据库中表的复杂查询
	1、连接查询
		1.0连接的基本语法格式:
			from TABLE1 join_type TABLE2 [on (join_condition)][where (query_condition)]
			TABLE1:左表
			TABLE2:右表
			join_type:连接的类型。交叉、内连接、左外连接、右外连接
			on:设置连接条件
			where:对连接查询的结果进步一的筛选
		1.1交叉连接
			select * from CUSTOMER cross join ORDERS;
			或者
			select * from CUSTOMER,ORDERS;
			
			select c.name,o.order_number from CUSTOMER c,ORDERS o;
		1.2内连接:
			隐式内连接:(不使用on关键字,使用where)
				select * from CUSTOMER c,ORDERS o where c.id=o.customer_id;
			显式内连接:(使用on关键字)
				select * from CUSTOMER c inner join ORDERS o on c.id=o.customer_id;
		1.3外连接:
			左外连接:(返回符合连接条件的所有记录,同时还返回左表中其余的所有记录)
				select * from CUSTOMER c left outer join ORDERS o on c.id=o.customer_id;
			右外连接:(返回符合连接条件的所有记录,同时还返回右表中其余的所有记录)
				select * from CUSTOMER c right outer join ORDERS o on c.id=o.customer_id;
	2、子查询(嵌套查询)
		子查询: select * from orders where customer_id=(select id from customer where name='张三');
	3、联合查询
		SELECT * FROM orders WHERE price>200 UNION SELECT * FROM orders WHERE customer_id=1;
		取两条语句的并集,并去除重复的记录。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇sedna加载xml文件 下一篇注意使用BTREE复合索引各字段的AS..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: