




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
SELECT语句高级用法1,使用group by 子句group by 子句将表分为几组,此子句通常与为每个这样的组生产总结值的聚集函数组合。使用不带聚集的group by 子句与在select 子句中使用的distinct(或unique)关键字很相似。select distinct customer_num from orders;selecct customer_num from orders group by customer_num;group by 子句将行收集到组中,因此每一组中的每一行具有相同的客户号。在没有选择任何其它列的情况下,结果是唯一的customer_num值的列表。select order_num,count(*) number,sum(total_price) price from items group by 1 order by 3;select o.order_num,sum(i.total_price) from order o,items i where o.order_date 01/01/98and ocustomer_num = 110 and o.order_num = i.order_num group by o.order_num;使用having子句要完成group by 子句,使用having 子句来在构成组之后将一个或多个限制条件应用于这些组。having子句对组的影响类似于where 子句限制个别行的方式,使用having子句的一个优点是可以在搜索条件中包括聚集,而在where子句的搜索条件中却不能包括聚集。每个having条件将组的一列或一个聚集表达式与组的另一个聚集表达式或与常量作比较。可以使用having来对列值或组列表中的聚集值设置条件。下面的语句返回具有两个商品以上的订单上每个商品的平均总价格。having子句在每个构成的测试组,并选择由两行以上构成的那些组。select order_num,count(*) number,avg(total_price) average from items group by order_num having count(*) 2;如果不带group by 子句使用having子句,则该having条件适应雨满足搜索条件的所有行。换言之,满足搜索条件的所有行构成一个组。select avg(total_price) average from items having count(*) 2;select o.order_num,sum(i.total_price) price, paid_date - order_date span from orders o,items iwhere o.order_date 01/01/98and o.customer_num 110and o.order_num = i.order_numgroup by 1,3having count(*) = 5*y.ship_dateand x.ship_date is not nulland y.ship_date is not nullorder by x.ship_date;如果想要将自连接的结果存储到临时表中,则将Into temp子句追加到select语句中,并至少对一组列指定显示标号,以重命名这些列。否则,重复列名将导致错误,并且不会创建临时表。select x.order_num orders1,x.po_num purch1,x.ship_date ship1,y.order_num orders2,y.po_num purch2,y.ship_date ship2from order x,orders y where x.ship_weight = 5*y.ship_weightand x.ship_date is not nulland y.ship_date is not nullorder by orders1,orders2into temp shipping;自连接子句三次select s1.manu_code,s2.manu_code,s3.manu_code,s1.stock_num,s1.descriptonfrom stock s1,stock s2,stock s3where s1.stock_num = s2.stock_numand s2.stock_num = s3.stock_numand s1.manu_code s2.manu_codeand s2.manu_code mgr.gross_payand emp.level (select count(*) from items b where b.total_price a.total_price )order by total_price;外连接外连接使其中一个表成为控制表(也成为外部表),控制其它从属表(也成为内部表)。在内连接或简单连接中,结果只保护满足连接条件的行组合,废弃部满足连接条件的行。在外连接中,结果包含满足连接条件的行与控制表中的行(如果在从属表中找不到匹配的行将废弃这些行)的组合。在从属表中没有相匹配的行的控制表在从属表选择的列中包含null值。外连接允许在应用连接条件之前将连接过虑器应用于内部表。ANSI外连接语法用left join,left outer join,right join和right outer join关键字开始外连接。outer关键字是可选的。查询可在on子句中指定连接条件和可选连接过虑器。where 子句指定后连接过虑器。左外连接在左外连接的语法中,外连接的控制表显示在开始外连接的关键字的左边。左外连接返回连接条件为true的所有行,除此之外,还返回控制表中的所有其它行并将从属表中的相应值显示为Null。select c.customer_num,c.lname,pany,c.phone,u.call_dtime,u.call_descr from customer c left outer join cust_calls u on c.customer_num = u.customer_num;select c.customer_num,c.lname,pany,c.phone,u.call_dtime,u.call_descrfrom customer c left outer join cust_calls u on c.customer_num = u.customer_numwhere u.customer_num is null;右外连接在右外连接的语法中,外连接的控制表显示在开始外连接的关键字右边,右外连接返回连接条件为true的所有行,除此之外,还返回控制表中的所有其它行并将从属表中的相应值显示为nullselect c.customer_num,c.fname,c.lname,o.order_num,o.order_date,o.customer_numfrom customer c right outer join orders o on (c.customer_num = o.customer_num);简单连接select c.customer_num,c.lname,pany,c.phone,u.call_dtime,u.call_descr from customer c,cust_calls uwhere c.customer_num = u.customer_num;对两个表的简单外连接select c.customer_num,c.lname,pany,c.phone,u.call_dtime,u.call_descr from customer c,outer cust_calls u where c.customer_num = u.customer_num;cust_calls表前面的附加关键字outer使该表成为从属表。外连接导致查询返回有关所有客户的信息,而不管他们是否已经致电客户服务中心,检索控制表customer的所有行,并且将Null值指定给从属表cust_calls的列。与第三个表进行简单连接的外连接这种外连接也称为嵌套简单连接select c.customer_num,c.lname,o.order_num,i.stock_num,i.manu_code,i.quantity from customer c, left outer join (orders o,items i)where c.customer_num = o.customer_numand o.order_num = i.order_numand manu_code in (KAR,SHM)ORDER BY lanme;将两个表与第三个表进行外连接作为两个表中的每一个与第三个表的外连接的结果的外连接。在此第三中类型的外连接中,连接关系可能仅仅使与从属表之间的关系。select c.customer_num,c.lname,o.order_num,order_date,call_dtime from customer c,outer orders o,outer cust_calls xwhere c.customer_num = o.customer_numand c.customer_num = x.customer_num order by lname into temp service;组合外连接的连接要实现多级嵌套,可以创建使用三种外连接类型的任何组合的连接。使用ansi语法,创建作为对两个表与另一个外连接的简单外连接组合结果的连接。select c.customer_num,c.lname,o.order_num,stock_num,manu_code,quantity from customer c,outer (orders o,outer items i)where c.customer_num = o.customer_numand o.order_num = i.order_numand manu_code in (KAR,SHM)ORDER BY lname;select 语句中的子查询下列情况定义数据库服务器支持子查询类型:1,嵌套在另一个select 语句的投影列表中的select语句2,嵌套在另一个select语句(或insert,delete或update语句中)的where子句中的select语句。相关子查询相关子查询使引用不在其from子句中的列或表的子查询。该列可以在projection子句或在where子句中。要查找查询引用的表,搜索不相关的列直到找到相关为止。projection子句中的子查询子查询可以在另一个select语句的投影列表中发生。select customer.customer_num,(select sum(ship_charge)from orders where customer.customer_num = orders.customer_num)as total_ship_chgfrom customer;where 子句中的子查询下来关键子在select语句的where子句中引入子查询allanyin exists使用all在子查询前面使用关键字all来确定对返回的每个值的比较是否为true.如果字查询不返回任何值,则搜索条件为true。(如果字查询不返回任何值,则对于所有零值条件为true)select order_num,stock_num,manu_code,total_pricefrom itemswhere total_price any (select total_price from items where order_num = 1005);单值子查询如果你指定子查询可能对外部级别查询返回刚好一个值,则不需要包括关键字all或any.可如同对待函数一样对待函数一样对待只返回一个值的子查询。这种子查询通常使用聚集函数,原因是聚集函数总是返回单个值。select order_num from items where stock_num = 9 and quantity = (select max(quantity) from items where stock_num = 9);相关子查询select po_num,ship_date from orders main where 10( select count (idstinct ship_date) from orders sub where sub.ship_date main.ship_date) and ship_date is not null order by ship_date,po_num;使用exists关键字exists也称为存在限定符,原因是仅当外部select找到至少一行时,子查询才为true.select unique manu_name,lead_time from manufact where exists(select * from stock where description mantches *shoe* and manufact.manu_code = stock.manu_code);集合运算标准集合运算联合,相交和差异允许你出来数据库信息。这三种运算允许你使用select语句在执行更新,插入或删除之后检查数据库的完整性。联合联合运算使用union关键字或运算符来将两个查询组合成单个符合查询。可以在两个或多个select语句之间使用union运算符来联合它们,并产生一个临时表,它包含存在于任何一个原始表或所有原始表中的行。还可以在视图的定义中使用union运算符。例子:select distinct stock_num,manu_code from stock where unit_price 3;select distinct stock_num,manu_code from stock where unit_price 3 order by 2;将order by 与 union 配合使用select distinct stock_num,manu_code from stock where unit_price 3 order by 2;使用union all缺省情况下,union关键字排除重复的行。要保留重复的行,添加可选的关键字allselect stock_num,manu_code from stock where unit_price 3 order by 2 into temp stock item;使用不同的列名select distinct state from customer where custo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO 18731:2025 EN Spices and condiments - Seasoning oil of Zanthoxyli pericarpium - Specification
- 学前口语考试试题及答案
- 深海养殖自动化喂养系统应用方案
- 混凝土砌块墙体施工技术方案
- 金属雕塑建筑施工组织设计及对策
- 光村镇2024-2025学年第二学期五年级科学期末学业评价题目及答案
- 农村饮水安全巩固提升工程施工方案
- 房屋建筑工程消防安全实施方案
- 碳捕集利用工程项目进度管理方案
- 宅基地空地租赁与农业项目合作合同书
- Dynaform中文手册文档
- 玉竹栽培技术课件
- 影视动画视听语言
- 线粒体肌病个案护理
- 煤矿掘进科培训课件
- 中医护理常规呕吐
- 粤语文化课件图片
- 2025至2030中国甲状腺素淀粉样变(ATTR)行业发展趋势分析与未来投资战略咨询研究报告
- 产品质量监督管理制度
- 失血性休克麻醉病例分享
- DB43-T 2724-2023 农村公路养护工程预算编制办法及定额
评论
0/150
提交评论