SQL第十五章

来源:互联网 发布:cygwin 连接linux 编辑:程序博客网 时间:2024/06/06 07:18
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><style type="text/css"><!--.STYLE1 {color: #FF0000}.STYLE2 { font-size: large; color: #0033FF;}body,td,th { color: #0000CC;}body { background-color: #CCCCCC;}.STYLE3 {font-size: large}--></style></head><body><h1 align="center"><span class="STYLE1">第十五章 Union和Union all </span></h1><p class="STYLE2">表的Union将两个表的行合并到了一个单个表中,且不需要对这些行做任何更改,两个表的行必须有相同的结构。即列的数量必须相同,并且相应列的数据类型必须相同。</p><p class="STYLE2">第一节</p><p class="STYLE2">Union和连接之间的区别:在Unionk,一个表的行必须可放到另一表中,结果表中列的数量与两个原表列的数量相同。在连接中,一个表的行可能与其它表的行有很大的不同。结果表可能包含来自于第一个和第二个表的列。在Union中,行的最大数量为两个表中的行的和。在连接中,行的最大数量是他们的乘积。连接和Union都不能自动获得主键。必须使用alter table为它们创建一个主键</p><p class="STYLE2">任务:给出Union与连接之间的一个例子</p><p class="STYLE2">select number_1,word_1,date_1</p><p class="STYLE2">from sec1407_second</p><p class="STYLE2">order by number_1;</p><p class="STYLE2">union</p><p class="STYLE2">select number_2,word_2,date_2</p><p class="STYLE2">from sec1407_second</p><p class="STYLE2">order by number_1;</p><p class="STYLE2"> </p><p class="STYLE2">select a.*,b.*</p><p class="STYLE2">from sec1407_first a,sec1407second b</p><p class="STYLE2">where a.number_1=b.number_2</p><p class="STYLE2">order by a.number_1;</p><p class="STYLE2">第二节 Union是另一种合并表的方法,与Union的惟一区别在于它不删除行也不对行进行自动排序。</p><p class="STYLE2">第五节,创建一个包含Union的视图或表。</p><p class="STYLE2">seclect *</p><p class="STYLE2">from sec1407_first</p><p class="STYLE2">union</p><p class="STYLE2">select *</p><p class="STYLE2">from sec1407_second order by 2;保存这个查询将其命名为sec1505a;</p><p class="STYLE2">select *</p><p class="STYLE2">in to sec1505b</p><p class="STYLE2">from sec1505a;</p><p class="STYLE2">在Access中,不能从Unionsjyqk直接创建一个表,首先创建包含Union的已保存的查询,然后创建一个表。</p><p class="STYLE2">在第六节</p><p class="STYLE2">Union的自动数据类型转换使得任何两个文本列,任何两个数字列是兼容的。</p><p class="STYLE2">非常规的Union</p><p class="STYLE2">作者认为任意两个表可以进行Uinon,原因是可以将所有类型的列中的数据转化 为文本。</p><p class="STYLE2">任务:显示如何在Union中使用数据类型转换函数。,</p><p class="STYLE2">select format(number_n) as first_colum n,</p><p class="STYLE2">text_t7 as second_column from ssec1506_forst</p><p class="STYLE2">union</p><p class="STYLE2">select text_t2,</p><p class="STYLE2">format(number_n2)</p><p class="STYLE2">from sec1506_secondm,第二步,使用GUi保存这个查询。</p><p class="STYLE2">第八节,对于有不同列数的两个表的Union,可以向列少的表添加一列null</p><p class="STYLE2">Union的应用:第一个</p><p class="STYLE2">确定两个表是不是相同。殂成两个表的Union,如果两个表相同,则Uniongng原表是有相同的行数,否则,Union将会有列多的行。</p><p class="STYLE2">第二个</p><p class="STYLE2">可以添加一列文字确定数据的来源。</p><p class="STYLE2">第三个</p><p class="STYLE2">标记异常情况,。任务:列出食品和它们的价格,将价格高于2美元的食品上添加“高价品“</p><p class="STYLE2">select description,</p><p class="STYLE2">price,</p><p class="STYLE2">'expensive item' as message</p><p class="STYLE2">from 1_foods </p><p class="STYLE2">where price>2</p><p class="STYLE2">union all</p><p class="STYLE2">select description,</p><p class="STYLE2">price,</p><p class="STYLE2">' '</p><p class="STYLE2">from 1_foods</p><p class="STYLE2">where not (price>2.00)</p><p class="STYLE2">or price is null</p><p class="STYLE2">order by description;</p><p class="STYLE2">第十二节 将数据从一个列分到两个不同的列中</p><p class="STYLE2">任务:将来自原表的cost列放以下两个列是中:debits,credits</p><p class="STYLE2">select item,</p><p class="STYLE2">null as debits,</p><p class="STYLE2">cost as credits</p><p class="STYLE2">from sec1512_finances</p><p class="STYLE2">where cost>0</p><p class="STYLE2">union all</p><p class="STYLE2">select intem,</p><p class="STYLE2">cost,</p><p class="STYLE2">null</p><p class="STYLE2">from sec1512_fnances</p><p class="STYLE2">where cost<0</p><p class="STYLE2">or cost is null</p><p class="STYLE2">order by item;</p><p class="STYLE2">任务 :找出存在于一个表中但不存在于其它表中的所有行。</p><p class="STYLE2">和十四章一样,左外连接挑出第二个为null.然后右外连接挑出第一个null.</p><p class="STYLE2">   </p><hr alogn=left width=80% size=2 color=green /><a href="首页.html" class="STYLE3">返回首页</a></body></html>

0 0
原创粉丝点击