SQL学习(三)

1. UNION 运算符

    实质就是关系运算中的并

   例:建立一个包含了顾客所在的或者代理商所在的或者两者接在的城市的名单

          select city from customers

                   union select city from agents;

当设计到的子查询项目大于或等于3时可用()

      (select city from customers

                union select city from agents)

                union all select city from products;

2. 除法: SQL "FOR ALL..."条件

         如果我们所面临的查询要求被检索的对象集合必须符合某个带有“所有”这类关键词的条件,我们就按照下列步骤进行

            1) 为要检索的对象命名并考虑如何用英文来表述要检索的候选对象的一个反例。在该反例中,前面提到的“所有”

                   对象中有至少一个对象不符合规定的条件。

            2) 建立Select语句的搜索条件以选出步骤1所创建的所有反例。(步骤1和2必定会引用来自外部Select语句的对象,

                   所以我们要在如何用这些外部对象所在的表来引用他们这一问题上表现出一定的灵活性)

            3)  建立包含步骤2所创建的语句的搜索条件,说明不存在上面定义的那种反例。这里将涉及到NOT EXISTS谓词

            4)  用步骤3的搜索条件来建立最终的Select语句,检索所期望的对象。

     上面的步骤序列通常会产生如下所示的嵌套子查询对:

            select ... where not exists (select ... where  not exists (select ... ));

      例:找出具有下列性质的顾客的cid值:如果顾客c006订购了某种产品,那要检索的顾客也订购了该产品。

          此题可以改写成:找出订购了所有被顾客c006订购的产品的cid值。

         步骤1:我们称c.cid是符合题意的顾客并用英文构造反例

                 "There is a product ordered by customer c006 that is not ordered by c.cid."

         步骤2:把英文表述成搜索条件,我们将被c006订购的产品命名为p.pid

                 cond1: p.pid in (select pid from orders x where x.cid='c006')

                                   and not exists (select * from orders y

                                           where y.pid=p.pid and y.cid=c.cid )

         步骤3:j建立表示这种反例不存在的条件:

                   cond2: not exists (select p.pid from prducts p

                                     where p.pid in (select pid from orders x

                                            where x.cid='c006')and

                                           not exists (select * from orders y

                                                 where y.pid=p.pid and y.cid=c.cid))

           步骤4:建立最终的Select

                           select cid from customers c

                                   where not exists (select p.pid from prducts p

                                     where p.pid in (select pid from orders x

                                            where x.cid='c006')and

                                           not exists (select * from orders y

                                                 where y.pid=p.pid and y.cid=c.cid));

           该select的一个变体是:

                              select cid from customers c

                                  where not exists  (select z.pid from oredrs z

                                        where z.cid='c006' and

                                         not exists (select * from orders y

                                         where y.pid=z.pid and y.cid = c.cid));

3.表别名用法

       例:检索对同一产品至少订购了两次的所有顾客的名字。

       select cname from (select o.cid as spcid from orders o,oreders x where o.cid=x.cid

                and o.pid=x.pid and o.ordno<>x.o.ordon)y, customers c

                 where y.spcid=c.cid;

该查询中为为子查询的结果指定了别名y,并为子查询所检索的列提供了别名spcid

posted @ 2011-03-04 20:13  一杯浓茶  阅读(315)  评论(0编辑  收藏  举报