sql连接查询问题总结

来源:互联网 发布:淘宝手机端怎么做推广 编辑:程序博客网 时间:2024/04/30 21:55



--------select hbsrsn, length(hbsrsn) from fxxx_jz_zk;  select hbsrsn,     case length(hbsrsn) when   20 then '国用户'                     else '地用户' end  用户类别    from fxxx_jz_zk   where hbsrsn = &hbsrsn;------join 连接select f.jf_lsh, f.tclx_mc, s.tclx, s.hmlx,case dm.tcje       when 0 then        substr(sd.sj_value, 1, instr(sd.sj_value, '元', 1) - 1)        else        to_char(dm.tcje,'9999')      end tcje,      decode(dm.tcje,            0,             substr(sd.sj_value, 1, instr(sd.sj_value, '元', 1) - 1),             dm.tcje) ttje  from   jfxx_zk f            join dm_tclx dm    on f.tclx_dm = dm.tclx_dm  join fxxx_jz_zk z    on f.jf_lsh = z.jf_lsh  left  join sjxx_jf_zk s        --- 连接查询记录 :员工不一定有部门,部门一定有员工 。     on (z.kpdw_dm = s.kpdw_dm and s.hmlx = 'Y')   ----on 条件 对于left join 只能作用 右表 即 z.jf_lsh=xxxx  对左表无效 left join dm_sjxx sd                             ----- 此处用 join 也行   on (s.tclx = sd.sj_key and sd.type_dm = '02')       where   dm.tcje = &tcje or substr(sd.sj_value,1,instr(sd.sj_value,'元',1)-1)= &tcje;      --sql有|| 吗。。。   -----(+) 连接查询select  nc.jf_lsh, nc.tclx_mc,nc.kpdw_dm, nc.hmlx, nc.tclx,nc.sj_key,nc.sj_value,nc.sjkpdw_dm, nc.tcjee, nc.ttje                                           -----未明确定义列   内查询中存在不同表相同字段名, 外查询不能区分  from (  select    f.jf_lsh, f.tclx_mc,z.kpdw_dm, sj.hmlx, sj.tclx,sj.sj_key,sj.sj_value,sj.kpdw_dm sjkpdw_dm, dm.tcje,      case dm.tcje       when 0 then        substr(sj.sj_value, 1, instr(sj.sj_value, '元', 1) - 1)        else        to_char(dm.tcje,'9999')      end tcjee,      decode(dm.tcje,            0,             substr(sj.sj_value, 1, instr(sj.sj_value, '元', 1) - 1),             dm.tcje) ttje  from   jfxx_zk f     , dm_tclx  dm      , fxxx_jz_zk z    , (select s.*, sd.* from sjxx_jf_zk s, dm_sjxx sd where  s.tclx = sd.sj_key  and sd.type_dm = '02' ) sj    where  f.tclx_dm = dm.tclx_dm  and  f.jf_lsh = z.jf_lsh  and  z.kpdw_dm = sj.kpdw_dm(+)     --至多外链接到其他一个表 不能再用(+)连接其他表 and  sj.hmlx(+) = 'Y'   )   nc  where                                                                                   nc.tcje = &tcje or   substr(nc.sj_value,1,instr(nc.sj_value,'元',1)-1)= &tcje;                ---or 或 in 不允许使用 (+) 单层查询无法实现该功能--------------------on 子句  和  where子句的区别select   z.hbsrsn, z.jf_lsh, s.sjhm from fxxx_jz_zk z left join sjxx_jf_zk s on z.kpdw_dm = s.kpdw_dm  and  s.sjhm <> '11171557711' where s.sjhm = '11171557711' order by z.yxrq    ---结果集为空  select   z.hbsrsn, z.jf_lsh, s.sjhm from fxxx_jz_zk z left join sjxx_jf_zk s on z.kpdw_dm = s.kpdw_dm  and z.jf_lsh <>'24155' where    z.jf_lsh ='24155'; order by z.yxrq  -------结果集 有一条记录select   z.hbsrsn, z.jf_lsh, s.sjhm from fxxx_jz_zk z  join sjxx_jf_zk s on z.kpdw_dm = s.kpdw_dm  and  z.jf_lsh <>'24155' where    z.jf_lsh ='24155' order by z.yxrq  --------结果集为后空总结:1. where字句作用于 表关联后的结果集(笛卡尔集),  而on 子句 作为表关联条件, and 后的子句( z.jf_lsh <>'24155') 也是关联条件            2. on 子句 作为表关联条件, and 后的条件子句( z.jf_lsh <>'24155') left join 时 只能作用于 右表, right join 只作用于左表, 内连接时 可作用 左右两表 


0 0
原创粉丝点击