我现在要查询shd表中的相关数据,其中hy(行业)字段可以为空,怎么将为空的也查询出来.
我现在的SQL语句只能将不为空的查询出来.
SQL语句如下:
select  city_num,b_year, qymc,suizhong.class_name as shuizhong,oper_def.oper_name as sgy
,swjg.jg_name as swjgname,bmhy.hy_name as hy,frxm, hyxm,
0 as total,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12 
from shd 
 inner join suizhong on shd.class_code=suizhong.class_code 
 inner join oper_def  on shd.sgy=oper_def.oper_num 
 inner join swjg on  shd.swjg=swjg.jg_code and shd.city_num=swjg.city_num
 inner join bmhy on shd.hy=bmhy.hy_code 
 where b_year='2007'  and qzdflag=0 and sgy='054033' 麻烦帮帮忙!!
急!!!!!!!!
谢谢!!!!! 

解决方案 »

  1.   

    到SQL版那里问。那里有很多高手
      

  2.   

    SELECT shd.city_num, shd.b_year, shd.qymc, 
           suizhong.class_name as shuizhong,
           oper_def.oper_name as sgy,
           swjg.jg_name   as   swjgname,
           bmhy.hy_name   as   hy,
           shd.frxm, shd.hyxm,
           0 as total,
           shd.s1, shd.s2, shd.s3, shd.s4, shd.s5, shd.
           shd.s6, shd.s7, shd.s8, shd.s9, shd.s10, shd.s11, shd.s12  
    FROM   shd, suizhong, oper_def, swjg, bmhy   
    WHERE shd.class_code = suizhong.class_code  
      AND shd.sgy        = oper_def.oper_num  
      AND shd.swjg       = swjg.jg_code   
      AND shd.city_num   = swjg.city_num
      AND shd.hy(+)      = bmhy.hy_code //用左外连接,可保留shd表中相应的空值字段;右外连接与此对称
      AND shd.b_year     = '2007'
      AND shd.qzdflag    = 0   
      and shd.sgy        = '054033'
      

  3.   


    SELECT shd.city_num,   shd.b_year,   shd.qymc,   
           suizhong.class_name   as   shuizhong, 
           oper_def.oper_name   as   sgy, 
           swjg.jg_name       as       swjgname, 
           bmhy.hy_name       as       hy, 
           shd.frxm,   shd.hyxm, 
           0   as   total, 
           shd.s1,   shd.s2,   shd.s3,   shd.s4,   shd.s5,   shd. 
           shd.s6,   shd.s7,   shd.s8,   shd.s9,   shd.s10,   shd.s11,   shd.s12     
    FROM   shd, suizhong, oper_def, swjg, bmhy       
    WHERE shd.class_code = suizhong.class_code     
      AND shd.sgy      = oper_def.oper_num     
      AND shd.swjg     = swjg.jg_code       
      AND shd.city_num = swjg.city_num 
      AND shd.hy(+)    = bmhy.hy_code //用左外连接,可保留shd表中相应的空值字段;右外连接与此对称 
      AND shd.b_year   = '2007' 
      AND shd.qzdflag  = 0       
      AND shd.sgy      = '054033'
      

  4.   

    SELECT shd.city_num, shd.b_year, shd.qymc,       
                  suizhong.class_name as shuizhong,   
                  oper_def.oper_name as sgy,   
                  swjg.jg_name as swjgname,   
                  bmhy.hy_name as hy,   
                  shd.frxm, shd.hyxm,   
                  0 as total,   
                  shd.s1, shd.s2, shd.s3, shd.s4, shd.s5, shd.s6,
                  shd.s7, shd.s8, shd.s9, shd.s10, shd.s11, shd.s12           
    FROM  shd, suizhong, oper_def, swjg, bmhy               
    WHERE shd.class_code = suizhong.class_code           
      AND   shd.sgy      = oper_def.oper_num           
      AND   shd.swjg     = swjg.jg_code               
      AND   shd.city_num = swjg.city_num   
      AND   shd.hy(+)    = bmhy.hy_code   //用左外连接,可保留shd表中相应的空值字段;右外连接与此对称   
      AND   shd.b_year   = '2007'   
      AND   shd.qzdflag  = 0               
      AND   shd.sgy      = '054033'
      

  5.   


    //原来如此
    SELECT shd.city_num, shd.b_year, shd.qymc,       
                  suizhong.class_name as shuizhong,   
                  oper_def.oper_name as sgy,   
                  swjg.jg_name as swjgname,   
                  bmhy.hy_name as hy,   
                  shd.frxm, shd.hyxm,   
                  0 as total,   
                  shd.s1, shd.s2, shd.s3, shd.s4, shd.s5, shd.s6,
                  shd.s7, shd.s8, shd.s9, shd.s10, shd.s11, shd.s12           
    FROM  shd, suizhong, oper_def, swjg, bmhy               
    WHERE shd.class_code = suizhong.class_code           
      AND   shd.sgy      = oper_def.oper_num           
      AND   shd.swjg     = swjg.jg_code               
      AND   shd.city_num = swjg.city_num   
      AND   shd.hy(+)    = bmhy.hy_code   //用左外连接,可保留shd表中相应的空值字段;右外连接与此对称   
      AND   shd.b_year   = '2007'   
      AND   shd.qzdflag  = 0               
      AND   shd.sgy      = '054033'
      

  6.   

    //这样,即使shd表中的hy字段是空值,那么包含该空值字段的记录也将被显示;如果是右外连接,由于shd中的hy字段无法在表bmhy中找到参照值,则shd表中的这条记录不会被返回。
    建议加深理解内、外、左、右连接的含义:)
      

  7.   

    难道是我搞反了?改过去看看SELECT shd.city_num, shd.b_year, shd.qymc,       
                  suizhong.class_name as shuizhong,   
                  oper_def.oper_name as sgy,   
                  swjg.jg_name as swjgname,   
                  bmhy.hy_name as hy,   
                  shd.frxm, shd.hyxm,   
                  0 as total,   
                  shd.s1, shd.s2, shd.s3, shd.s4, shd.s5, shd.s6,
                  shd.s7, shd.s8, shd.s9, shd.s10, shd.s11, shd.s12           
    FROM  shd, suizhong, oper_def, swjg, bmhy               
    WHERE shd.class_code = suizhong.class_code           
      AND   shd.sgy      = oper_def.oper_num           
      AND   shd.swjg     = swjg.jg_code               
      AND   shd.city_num = swjg.city_num   
      AND   shd.hy       = bmhy.hy_code(+)      
      AND   shd.b_year   = '2007'   
      AND   shd.qzdflag  = 0               
      AND   shd.sgy      = '054033'
      

  8.   

    恩,现在可以了
    谢谢!
    麻烦你在帮我看下另外个问题
    我做了个查询窗体,       
    查询的条件比较多:州地市,单位,税务机关,税管员,税种,行业,行业明细       
    这些条件都是用下拉组合框(combobox)来显示的。       
    现在的要求是:       
    当我用“成都市市”用户登录系统的时候,该窗体中的州地市就被选定为“成都市”相应的后面的单位组合框就只能显示“成都市的单位”   
    当我用“四川省省级”用户登录系统的时候,该窗体中的州地市就被选定为“四川省”相应的后面的单位组合框就只能显示“四川省的单位”   
    后面的组合框也是如此。州地市级联单位;单位级联税务机关;税务机关级联税管员。 我不知道该怎么实现。还请各位高手帮帮忙! 谢谢! 急!!!!!!!!! 谢谢!!! 
      

  9.   

    procedure   TForm2.FormShow(Sender:   TObject); 
    var   s1,s2,s3,s4,s5,s6,z1,z2:string; 
      regisname:Tregistry   ; 
      Y,M,D:Word; 
    begin 
    //Form_xz.Top:=100000; 
        DecodeDate(Date,Y,M,D); 
        spinedit1.Value:=Y; 
        spinedit2.Value:=M; 
        spinedit3.Value:=M; 
        Regisname:=Tregistry.create   ; 
        Regisname.openkey('hxsoft\czy',true)   ; 
        z1:=regisname.readstring('dm')   ; 
        z2   :=regisname.readstring('Jg')   ; //州地市 
    LJClientDataSet2.Close; 
    LJClientDataSet2.Provider.DataRequest( 
    'select   zds_code   ,zds_name   from   dsbm.bmzds   '+ 
    'where   zds_code   in   (select   substr(city_num,1,4)   from   dsbm.oper_def   where   oper_num='+z1+'   )   /'); 
    LJClientDataSet2.Open; 
    while   not   LJClientDataSet2.Eof   do 
    begin 
    combobox1.Items.Add(LJClientDataSet2.fieldbyname('zds_code').asstring+'['+LJClientDataSet2.fieldByName('zds_name').asstring+']'); 
    LJClientDataSet2.Next; 
    end; 这是我的代码,但是会出错。 
    麻烦指点下。 
    谢谢!! 错误提示是: 
    project   project1.exe   raised   exception   class   EOLeException   with   message   'General   SQL   error.   ORA-01722:invalid   number'.Precess   stopped   Use   Step   or   Run   to   continue. 我也不知道什么原因 我在数据库里面用SQL:select   *   from   bmzds   where   zds_code   in   (select   substr(city_num,1,4)   from   dsbm.oper_def   where   oper_num='054033') 
    都能查询出结果 
    z1='054033'
      

  10.   

    用下拉列表框的OnChange事件结合数据库实现:
    当第一个下拉框的值为“成都”时,在该下拉框的OnChange事件中查询相应的下级单位显示到第二个下拉框。
    //参考下列代码procedure TForm1.ComboBox1Change(Sender: TObject);
    begin
      with ComboBox2, TADOQuery.Create(nil) do
      try
        Connection := ADOConnection1; //ADO连接
        Close;
        SQl.Text := ' SELECT 单位名称 FROM 单位表 ' +
                    ' WHERE 上级单位代码 = ' + QuotedStr('ChengDu');
                    //或者' WHERE 上级单位 = ' + QuotedStr('成都');
        Open;    ComboBox2.Clear;
        while not Eof do
        begin
          ComboBox2.Items.Add(FieldByName('单位名称').AsString);
          Next;
        end;
        Close;
      finally
        Free;
      end;
    end;
      

  11.   

    'General       SQL       error.       ORA-01722:invalid       number'
    这个错误发生原因是:涉及到查询的某个表中的字段被定义为数值型——据我观察,很可能就是oper_num,但是你后面的SQL语句跟的是一个字符串(where oper_num = '+z1+'...  ),检查一下