4.连接查询

1.什么是连接查询?
2.内连接;
3.外连接;
4.交叉连接;
5.自连接;
6.联合查询;

一.只要涉及两个或者两个以上的表,就称为连接查询.
比如:查询出女客户号购买了哪些商品,即由客户表查出女性客户结果集作为条件,然后再在明细表中查询出客户号;
    select 商品ID    --应该有重复行,消除重复行distinct
    from 明细表
    where cno in  --除了in之外,也可以使用=any 或者=some
    {
        select cno
        from 客户表
        where csex='女'
    }
    这是最明显的嵌套,但思维比较复杂,如果能把两张表合成一个结果集,查询就简单明了.
    如:先把两个查询语句一起执行.
        select * from 客户表
        select * from 明细表
    查看结果:...
    select *
    from 客户表 as t1 ,明细表 as t2
    where
     有了这个结果集就更容易了.那就应该写成:
     select distinct 商品ID
     from 客户表 as t1 ,明细表 as t2
     where t1.cno=t2.cno and t1.csex='女'
     这和上面的查询结果是一致的.把多个表组合成一个大表!!!
     还可以使用inner join ...on来实现:
     select distinct 商品ID
     from 客户表 as t1 inner join 明细表 as t2
     on t1.cno=t2.cno
     where t1.csex='女'

 --内连接:对两个表进行连接查询,满足条件的显示出来,不满足的舍弃.举例:有两个客户表,只有客户号有相同的,字段和记录个数都不相同,看看查询结果:
     --两条语句一起执行:
     select * from  客户表1
     select * from  客户表2
     --组合查询,两个表的相同的也在一个结果集中了
     select *
     from 客户表1,客户表2
     where 客户表1.cno=客户表2.cno
     --内连接查询,和上面的结果一样
     select *
     from 客户表1 inner join 客户表2 on 客户表1.cno=客户表2.cno
--外连接(左外连接,右外连接和全连接),和内连接不同的是根据哪张表连接.比如:
    --左外连接
     select * from  客户表1
     select * from  客户表2
     select *
     from 客户表1 left outer join 客户表2 on 客户表1.cno=客户表2.cno
     --右外连接,就是把left换成right,是以第二个表为基准.
     --全连接,就是把left 换成full,是把两个表全部叠加起来.

     练习 :
     1.查询出女客户购买了哪些商品?
     2.查询出哪些被购买的商品中产地是北京的?intersect也可以用内连接(生产厂家表)
     3.查询出哪些人从来没购买过商品?

    
     select 商品名称
     from
     (
         select distinct 商品ID
         from 客户表 as t1 inner join 明细表 as t2
         on t1.cno=t2.cno
         where t1.csex='女'
     ) as t3 inner join 商品表 as t4 on t3.商品ID=t4.商品ID

     也可以把三张表连接起来:
     select distinct 商品名称 from 客户表 as t1
     inner join
     明细表 as t2 on t1.cno=t2.cno
     inner join
     商品表 as t3 on t2.商品ID=t3.商品ID
     where t1.csex='女'

     第2题:最容易想到的方法就是求北京厂家生产的商品和被购买过的商品的交集.
         --产地为北京的商品名称集合
         (
             select 商品名称
             from 商品表
             where 生产厂家 in
             (
                 select 商品ID
                 from 厂家表
                 where 厂家所在地市='北京'
             )
         )
         intersect  --求交集 union并集
         --被购买过的商品名称集合
         (
             select 商品名称
             from 商品表
             where 商品ID in
             (
                 select distinct 商品ID
                 from 明细表
             )
         )
--使用inner join
    select distinct 商品名称
    from 商品表 as t1
    inner join
    厂家表 as t2 on t1.生产厂商 = t2.厂家ID
    inner join
    明细表 as t3 on t1.商品ID  = t3.商品ID
    where t2.厂家所在地市='北京'

第3题:
    1.select 客户姓名
    from 客户表
    where  客户ID not in
    (
        select distinct 客户ID
        from 明细表
    )
    2.
    select 客户姓名
    from 客户表
    where 客户ID in
    (
        (
            select 客户ID
            from 客户表
        )
        except--除...之外
        (
            select distinct 客户ID
            from 明细表
        )
    )
    3.连接查询
    select 客户姓名    from 客户表 as t1
    left outer join
    明细表 as t2 on t1.客户ID = t2.客户ID
    where 购买日期 is null
    

   

原文地址:https://www.cnblogs.com/yangwenlong/p/7905757.html