SQL 必知必会10-14

10章

–创建分组 查找列中id,重复次数
–select vend_id,count(*) as num_prods
–from Products
–group by vend_id

–过滤分组 找出vend-id 重复大于2的数据 having 过滤分组
–select vend_id,count() as num_prods
–from Products
–group by vend_id
–having COUNT(
) >2

–select vend_id,COUNT() as num_prods
–from Products
–where prod_price >=4
–group by vend_id
–having COUNT(
) >=2;

–ORDER BY 与 GROUP BY
– 对产生的输出排序 对行分组,但输出可能不是分组的顺序
–任意列都可以使用(甚至非选择的列也可以使用) 只可能使用选择列或表达式列,而且必须使用每个选择列表达式
–不一定需要 如果 与 聚集 函数 一起 使用 列( 或 表达式),

–select order_num ,COUNT() as items
–from OrderItems
–Group by order_num
–having COUNT(
) >=3
–order by items,order_num

–SELECT 子句 及其 顺序
–子     句 说     明 是否 必须 使用
–SELECT 要 返回 的 列 或 表达式 是
–FROM 从中 检索 数据 的 表 仅在 从 表 选择 数据 时 使用
–WHERE 行 级 过滤 否
–GROUP BY 分组 说明 仅在 按 组 计算 聚集 时 使用
–HAVING 组 级 过滤 否
–ORDER BY 输出 排序 顺序 否

11章

–利用子查询进行过滤
–select cust_id
–from Orders
–where order_num in(select order_num
–from OrderItems
–where prod_id=‘FB’)

–作为计算机字段使用子查询
–select COUNT(*) as orders
–from Orders
–where cust_id=‘10003’

–select cust_name,
–cust_state,
–(select COUNT(*)
–from Orders
–where Orders.cust_id=Customers.cust_id) as orders
–from Customers
–order by cust_name

12章

--创建连接
–select vend_name,prod_name,prod_price
–from Vendors,Products
–where Vendors.vend_id=Products.vend_id

–内连接
–select vend_name,prod_name,prod_price
–from Vendors inner join Products
–on Vendors.vend_id=Products.vend_id

–连接多个表
–select prod_name,vend_name,prod_price,quantity
–from OrderItems,Products,Vendors
–where Products.vend_id = Vendors.vend_id
–and OrderItems.prod_id=Products.prod_id
–and order_num=20005

13章

–使用表的别名
–select cust_name,cust_contact
–from Customers as C,Orders as O,OrderItems as OI
–where C.cust_id=O.cust_id
–and OI.order_num=O.order_num
–and prod_id=‘FB’

–子连接
–select c1.cust_id,c1.cust_name,c1.cust_contact
–from Customers as c1,Customers as c2
–where c1.cust_name=c2.cust_name
–and c2.cust_contact=‘Y Lee’

–自连接
–select c1.cust_id,c1.cust_name,c1.cust_contact
–from Customers as c1,Customers as c2
–where c1.cust_name=c2.cust_name
–and c2.cust_contact=‘Y Lee’

–自然连接 查询产品 FB 被哪些客户买了,买了多少
–select c.*,O.order_num,o.order_date,OI.prod_id,OI.quantity,OI.item_price
–from Customers as C,Orders as O,OrderItems as OI
–where C.cust_id=O.cust_id
–and OI.order_num=o.order_num
–and prod_id =‘FB’

14章

14 组合查询
–可用 UNION 操作符来组合数条SQL查询。利用UNION,可给出多条SELECT语句,将它们的结果组合成一个结果集。
–使用UNION很简单,所要做的只是给出每条 SELECT 语句,在各条语句之间放上关键字 UNION。 举个例子,假如需要 Illinois、Indiana 和 Michigan 等美国几个州
–的所有顾客的报表, 还想包括不管位于哪个州的所有 的 Fun4All。 当然可以利用 WHERE 子句来完成此工作, 不过这次我们使用 UNION。
–select cust_name,cust_contact,cust_email,cust_state
–From Customers
–where cust_state in (‘IL’,‘IN’,‘MI’)

–select cust_name,cust_contact,cust_email,cust_state
–from Customers
–where cust_name=‘Wascals’

–UNION 是将where 结合到一块的查询 UNION 查询结果集中自动去除重复的行, UNION ALl 不取消重复的行
–select cust_name,cust_contact,cust_email,cust_state
–From Customers
–where cust_state in (‘IL’,‘IN’,‘MI’)
–union
–select cust_name,cust_contact,cust_email,cust_state
–from Customers
–where cust_name=‘Wascals’

–UNION 规则
–可以看到, UNION 非常容易使用, 但在进行组合时需要注意几条规则。 UNION 必须由两条或两条以上的SELECT 语句组成, 语句之间用关键字UNION 分隔(因此,如果组合四条 SELECT
–语句,将要使 三个UNION 关键字)。UNION 中的每个查询必须包含相同的 列、表达式或 聚集函数( 不过,各个列不需要以相同的次序 列出)。列数据类型必须兼容: 类型不必完全相同,
–但必须是DBMS可以隐含转换的 类型( 例如, 不同 的 数值 类型 或 不同 的 日期 类型)。 如果 遵守 了 这些 基本 规则 或 限制, 则 可以 将 UNION 用于 任何 数据 检索
–操作。

–对组合查询结果排序
–SELECT 语句的输出用ORDER BY 子句排序。 在用UNION 组合查询时,只能使用一条 ORDER BY 子句, 它必须位于最后一条 SELECT 语句 之后。
–对于 结果 集,不存在用一种方式排序一部分,而又用另一种方式排序另一部分的况,因此不允许使用多条 ORDER BY 子句。

–select cust_name,cust_contact,cust_email
–from Customers
–where cust_state IN (‘IL’,‘IN’,‘MI’)
–UNION
–select cust_name,cust_contact,cust_email
–from Customers
–where cust_name=‘wascals’
–order by cust_name,cust_contact

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值