sql小结

(案例来自医药采购)

 1.1     内链接关联查询:(一对一:只能查询到一条数据)

  如果表A和表B有一个外键关联 (由于有外键可以通过内链接),可以通过外键进行内链接查询

  select dictinfo.*,dicttype.typename

  from dictinfo, dicttype

 where dictinfo.typecode = dicttype.typecode

 

  --不通过外键,通过groupid查询用户类型的代码结果集,只能查询出一条记录,可以使用内链接

selectsysuser.*, dictinfo.info

  from sysuser,

       (select dictcode, typecode, info from dictinfo where typecode = 's01') dictinfo

 where sysuser.groupid = dictinfo.dictcode

 

小结:如果主查询表字段从关联表只查询出一条记录,这个字段就可以作为内链接关联字段

 

--内链接的错误的例子,通过关联查询出重复记录

--使用groupidselect dictcode, typecode, info fromdictinfo可以找到多个记录,不能使用内链接,可能会出现重复记录

 

selectsysuser.*

  from sysuser, (select dictcode, typecode, info from dictinfo) dictinfo

 where sysuser.groupid = dictinfo.dictcode

 

 

注意:如果使用内链接查询出现重复记录,首先去思考是否是sql写错了,不能直接去使用distinct去除重复记录

有一些特殊情况下还是需要使用distinct去除重复记录,比如复杂的统计分析sql。

 

1.2     外链接关联查询:

 

表A,表B中只有一部分数据和表A匹配,不能使用内链接。

主查询是表A,只能使用外链接。

 

--查询用户所属单位,sysid对应三张表的id

 

 

左外链接   left 左边的为主查询表全部显示  右边为关联查询表(部分字段) on 后边为要关联的字段

selectsysuser.*,useryy.mc from sysuser leftjoin useryy on sysuser.sysid =useryy.id

 

右外链接right右边主查询表全部显示左边为关联查询表(部分字段) on后边为要关联的字段

 

select * from useryy rightjoin sysuser on sysuser.sysid =useryy.id

 

--以上的需要不能使用内链接

selectsysuser.*,useryy.mc from sysuser,useryy where  sysuser.sysid = useryy.id

  

小结:

表A中从表B中只能关联查询一部分数据,只能使用外链接

1.3     子查询

 

selectsysuser.*,

 (select * from useryywhereid = sysuser.sysid)

 from sysuser

子查询只能返回一列,否则:报错

 sql小结_第1张图片

子查询只允许返回一行,否则

sql小结_第2张图片 

正确的sql:

--子查询

--根据sysid取出单位名称

--根据groupid查询用户类型代码对应的名称

selectsysuser.*,

 (select mcfrom useryy whereid =sysuser.sysid)sysmc,

 (select info from dictinfo where dictcode = sysuser.groupid and typecode = 's01')groupname

 from sysuser

1.4     嵌套表

 

可以将一个sql查询结果组成一个虚表,查询方式和查询一个实体表相同的。

组成的虚拟表字段是不允许重复的,否则 :

 sql小结_第3张图片

2      notin 关键字

 

select

ypxx.id,

       ypxx.bm,

       ypxx.mc,

       ypxx.jx,

       ypxx.gg,

       ypxx.zhxs,

       ypxx.scqymc,

       ypxx.spmc,

       ypxx.zbjg,

       ypxx.jyzt,

      

       (select info

          from dictinfo

         where ypxx.jyzt = dictcode

           and typecode = '003') jyztmc

 from ypxx

 

-- notin 将供货商药品目录已经存在药品过虑掉not后括号里的查询结果过滤掉

--ypxx表中记录,根据ypxxid关联,不在select ypxxid from gysypml whereusergysid='5197cdd2-08cf-11e3-8a4f-60a44cea4388'结果集中

where ypxx.id notin(

   --某个供货商药品目录结果集

   select ypxxid from gysypml where usergysid='5197cdd2-08cf-11e3-8a4f-60a44cea4388'

)

3      not exists  关键字

--

 

select ypxx.id,

       ypxx.bm,

       ypxx.mc,

       ypxx.jx,

       ypxx.gg,

       ypxx.zhxs,

       ypxx.scqymc,

       ypxx.spmc,

       ypxx.zbjg,

       ypxx.jyzt,

      

       (select info

          from dictinfo

         where ypxx.jyzt = dictcode

           and typecode = '003') jyztmc

--子查询,关联查询到说明此药品id在供货商药品目录存在

--(select id from gysypml whereusergysid='5197cdd2-08cf-11e3-8a4f-60a44cea4388' and ypxx.id = gysypml.ypxxid)gysypmlid

  from ypxx

--查询子查询不为空的值

 wherenotexists (selectid

          from gysypml

         where usergysid = '5197cdd2-08cf-11e3-8a4f-60a44cea4388'

           and ypxx.id = gysypml.ypxxid)

 

--每个记录查询时,都需要执行exists中的子查询

--建议子查询条件根据主键或索引查询,可以提高效率,推荐使用exists,因为exists比in或not in速度要高

4      in  关键字

select

    gysypml.id gysypmlid,

    gysypml.ypxxid,

    gysypml.usergysid,

    usergys.mc usergysmc,

    gysypml_control.control,

    (select info

    from dictinfo

    where typecode = '008'

    and dictcode = gysypml_control.control)controlmc,

 

    ypxx.id,

    ypxx.bm,

    ypxx.mc,

    ypxx.jx,

    ypxx.gg,

    ypxx.zhxs,

    ypxx.scqymc,

    ypxx.spmc,

    ypxx.zbjg,

    ypxx.jyzt,

 

    (select info

    from dictinfo

    where ypxx.jyzt = dictcode

    and typecode = '003') jyztmc

 

    from gysypml, usergys, gysypml_control,ypxx

    where gysypml.usergysid = usergys.id

    and gysypml.ypxxid =gysypml_control.ypxxid

    and gysypml.usergysid =gysypml_control.usergysid

    and gysypml.ypxxid = ypxx.id

   

    --限制只查询医院本区域供货商的药品目录

    --1.1.16.是医院所在区域

   

    and gysypml.usergysid in (

       select usergysarea.usergysid from usergysarea where'1.1.16.'like usergysarea.areaid|| '%'

    )

    --将采购单中药品过虑掉

    --2014101040就是采购单id

    and gysypml.ypxxid notin(

        select yycgdmx.ypxxid from yycgdmx2014 yycgdmxwhere yycgdmx.yycgdid = '2014101040'

    )

   

    --分析所需要的子查询

   

    --采购单下药品id列表

   select yycgdmx.ypxxid from yycgdmx2014 yycgdmx where yycgdmx.yycgdid ='2014101040'

   

    --查询出医院所在区域供货商的信息

   select usergysarea.usergysid from usergysarea where '1.1.16.' likeusergysarea.areaid || '%'

   

    --查询医院的区域(三级区域1.1.16.)

   select dq from useryy where id='1f8b098b-067e-11e3-8a3c-0019d2ce5116'

  

 

 

5       sum  关键字

 

sum 求和  ,nvl  如果 该字段为空则按0 计算

   select  sum(nvl(yycgdmx.cgl,0)) cgl,

     sum(nvl(yycgdmx.cgje,0)) cgje

    

    from yycgdmx2015 yycgdmx,

    yycgd2015 yycgd, useryy,

    ypxx, usergys

    where yycgdmx.yycgdid

    = yycgd.id

    and yycgd.useryyid = useryy.id

    and yycgdmx.ypxxid = ypxx.id

and yycgdmx.usergysid = usergys.id

 

 

6      groupby  关键字

groupby  按照什么统计

group by business.id,business.bm,business.mc  先按照id 统计

如果business.bm列有重复按照business.bm统计

如果business.mc列有重复然后按照business.mc统计

 

 

select

 business.id,

 business.bm,

 business.mc,

 sum(nvl(business.cgl,0))cgl,

 sum(nvl(business.cgje,0))cgje,

 sum(nvl(business.rkl,0))rkl,

 sum(nvl(business.rkje,0))rkje,

 sum(nvl(business.thl,0))thl,

 sum(nvl(business.thje,0))thje,

 sum(nvl(business.jsl,0))jsl,

 sum(nvl(business.jsje,0))jsje

 from (

selectuseryy.id      useryyid,

       useryy.mc      useryymc,

       yycgd.bm       yycgdbm,

       yycgd.id       yycgdid,

       usergys.id     usergysid,

       usergys.mc     usergysmc,

       yycgdmx.ypxxid,

       ypxx.id,

       ypxx.bm,

       ypxx.mc,

       ypxx.jx,

       ypxx.gg,

       ypxx.zhxs,

       ypxx.scqymc,

       ypxx.spmc,

      

       ypxx.jyzt,

      

       (select info

          from dictinfo

         where ypxx.jyzt = dictcode

           and typecode = '003') jyztmc,

       (select info

          from dictinfo

         where typecode = '011'

           and dictcode = yycgdmx.cgzt) cgztmc,

       yycgdmx.cgl,

       yycgdmx.cgje,

       yycgdmx.rkl,

       yycgdmx.rkje,

       yycgdmx.thl,

       yycgdmx.thje,

       yycgdmx.jsl,

       yycgdmx.jsje

 

  from yybusiness2014 yycgdmx, yycgd2014yycgd, useryy, usergys, ypxx

 where yycgdmx.yycgdid = yycgd.id

   and yycgd.useryyid = useryy.id

   and yycgdmx.usergysid = usergys.id

   and yycgdmx.ypxxid = ypxx.id

     

      --监管单位查询管理地区内医院采购明细信息

     

   and useryy.id in (

                     --管理地区内医院

                     selectidfrom useryy where dq like'1.1.%')

     

      --医院查询自己的采购明细信息

   and useryy.id = '1f8b098b-067e-11e3-8a3c-0019d2ce5116'

     

      --供货商查询:与本供货商相关的采购明细信息

   and usergys.id = '5197cdd2-08cf-11e3-8a4f-60a44cea4388'

  

   )business

  

   --分类统计

   --按药品统计

  groupbybusiness.id,business.bm,business.mc   

  

 

7      实现(用例)

查询出所有镇区域内的采购金额没有采购的以0 计算

select areainfo.areaname,

nvl(yybusiness.cgje,0)cgje

 

  from (select areaname, 0 cgje

          from bss_sys_area

         where bss_sys_area.arealevel = '2') areainfo

  leftjoin (

            

             select yybusiness.areaname, sum(yybusiness.cgje) cgje

               from (select yybusiness.*,

                             (select bss_sys_area.areaname

                                from bss_sys_area

                               where areaid = yybusiness.parentid) areaname

                        from (select yybusiness.*,

                                     (select parentid

                                        from bss_sys_area

                                       where areaid = yybusiness.useryydq) parentid

                                from (select yybusiness.useryyid,

                                             (select dq

                                               from useryy

                                               whereid = yybusiness.useryyid) useryydq,

                                            

                                            yybusiness.cgje

                                        from (

                                             

                                              select useryy.id useryyid,

                                                      useryy.mc useryymc,

                                                     yycgd.bm yycgdbm,

                                                     yycgd.id yycgdid,

                                                     usergys.id usergysid,

                                                     usergys.mc usergysmc,

                                                     yycgdmx.ypxxid,

                                                     ypxx.id,

                                                      ypxx.bm,

                                                     ypxx.mc,

                                                     ypxx.jx,

                                                     ypxx.gg,

                                                      ypxx.zhxs,

                                                     ypxx.scqymc,

                                                     ypxx.spmc,

                                                     

                                                     ypxx.jyzt,

                                                     

                                                     (select info

                                                        from dictinfo

                                                       where ypxx.jyzt = dictcode

                                                         and typecode = '003') jyztmc,

                                                     (select info

                                                         from dictinfo

                                                       where typecode = '011'

                                                         and dictcode = yycgdmx.cgzt)cgztmc,

                                                      yycgdmx.cgl,

                                                     yycgdmx.cgje,

                                                     yycgdmx.rkl,

                                                     yycgdmx.rkje,

                                                      yycgdmx.thl,

                                                     yycgdmx.thje,

                                                     yycgdmx.jsl,

                                                     yycgdmx.jsje

                                             

                                               from yybusiness2015 yycgdmx,

                                                     yycgd2015 yycgd,

                                                     useryy,

                                                     usergys,

                                                     ypxx

                                               where yycgdmx.yycgdid = yycgd.id

                                                and yycgd.useryyid = useryy.id

                                                and yycgdmx.usergysid = usergys.id

                                                and yycgdmx.ypxxid = ypxx.id

                                             

                                              )yybusiness) yybusiness) yybusiness) yybusiness

              groupby yybusiness.areaname) yybusiness on yybusiness.areaname =

                                                         areainfo.areaname

 

 

 

你可能感兴趣的:(sql)