GBase 单表、多表、嵌套、高级查询

餐前小点心:

排序:可以使用order by 对查询结果按照一个或多个属性列的升序或降序排列。
聚合函数:是以一个值的集合为输入,返回单个值的函数。注意聚合函数COUNT、SUM、AVG、MAX、MIN都忽略空值而只处理非空值,只有COUNT(*)将空值计算在内
分组:使用group by子句可以将查询结果按照某一列或者多列的值分组,值相等的分为一组。对查询结果分组的目的是细化聚合函数的作用对象;如果只对分组后的某些组感兴趣,则可以使用HAVING关键字指定筛选条件,从而值输出满足指定条件的组,实现对组的筛选。
having 和 where的区别:having和where的作用对象不同。where子句作用于基本表或视图,即元组的集合,从中选择满足条件的行;而having关键字作用于组的集合,从中选择满足条件的组。

单表查询

只在一个表中做增删改查的操作....

多表查询

内连接

查询每位顾客的联系方式及其订货日期:

	select
		customer_num,
		cname,
		phone,
		order_num,
		order_date
	from
		customer join orders
	on
		customer.customer_num = orders.customer_num;

外连接

内连接操作只会输出两个表中在连接谓词上匹配到的行。例如,在上次查询中,就没有输出103和105号顾客的信息,因为其现在暂时没有订单,在order表中没有相应的订单记录,造成customer表的这一行与orders表的任意一行在连接谓词上都不匹配。
# 查询每位顾客的订购物品的信息,目前没有订单的顾客也要列出。
	select
		customer.customer_num,
		phone,
		cname,
		order_num,
		order_date
	from
		customer ledt outer join orders
	on customer.customer_num = orders.customer_num;

右外连接

与左外连接相反,右外连接的结果会返回表中的全部行。

完全外连接

完全左外连接和右外连接的结果组合在一起,形成的结果表包含左表和右表周玲全部的行。
没有匹配的属性值使用null填充。完全外连接使用full outer join关键字

自连接

自连接是一个表与自身进行连接操作。自连接并不是一种新的连接类型,它实际上是一
种特殊形式的内连接。
# 查询提交一份以上订单的顾客信息
	select
		distinct A.customer_num
	from orders A inner join orders B
	on A.customer_num = B.customer_num
	where A.order_num <> B.order_num;

或
	select
		distinct A.customer_num
	from orders A, Orders B
	where A.customer_num = B.customer_num
	and A.order_num <> B.order_num;

集合查询

# 查询单价大于400或数量大于3的订单明细
	select *
	from items
	where ship_price > 400
	union
	select *
	from items
	where quantity >3;

高级查询

	select
		name,
		employee_id,
		manager_id
	from
		employee
	start with name = "张一川"
	connect by prior employee_id = manager_id;

posted @ 2022-07-22 14:18  乐酷yuan  阅读(461)  评论(0编辑  收藏  举报