String where = "where 1=1";
 if(!zyName.equals("")){
     where = where + " and zyName ='"+zyName+"' ";
  }  
  if(!source.equals("")){
     where = where + " and source like '%"+source+"%'";
  }
Page pagecx=db.selectAllRowsPage("select zyId,zyName,otherName,source,zucheng,useMethod,zhuzhi,bz from zy_info"+ where); 
想在sql中加入where 但是怎么弄呢???

解决方案 »

  1.   

    "select zyId,zyName,otherName,source,zucheng,useMethod,zhuzhi,bz from zy_info"+ where
    你这个sql语句不是加了where吗???
      

  2.   

    在引号里面就接着写就可以了啊!
    "select zyId,zyName,otherName,source,zucheng,useMethod,zhuzhi,bz from zy_info where 你的条件判定比如 zyId=1"
      

  3.   


    /*
    String where = "where 1=1";
     if(!zyName.equals("")){
         where = where + " and zyName ='"+zyName+"' ";
      }  
      if(!source.equals("")){
         where = where + " and source like '%"+source+"%'";
      }
    Page pagecx=db.selectAllRowsPage("select zyId,zyName,otherName,source,zucheng,useMethod,zhuzhi,bz from zy_info"+ where); 
    想在sql中加入where 但是怎么弄呢???
    */select zyId,zyName,otherName,source,zucheng,useMethod,zhuzhi,bz 
    from zy_info 
    where 1 = 1 and source like '%???%'   --???表示你那个参数值!
      

  4.   

    嗯 我的意思是where是动态生成的 相当于一个变量 把他直接加载sql语句里 不是sql里面原有where
      

  5.   


    --按楼主的想法可以用动态SQL实现create proc get_where(@where varchar(1000))
    as
    being
    declare @sql varchar(8000)
    set @sql = 'select zyId,zyName,otherName,source,zucheng,useMethod,zhuzhi,bz from zy_info'
    set @where = ' where 1 = 1'
    if(/*楼主要判断的条件*/)
        set @where = @where + 'source like ''%...%'''
    set @sql = @sql + @where
    exec(@sql)
    end
    goexec get_where 'aaa'
      

  6.   

    上边打错,是as
    begin
    今天老打错字,别介意!
      

  7.   

    上边打错,是as
    begin
    今天老打错字,别介意!
      

  8.   

    那可不可以直接写在sql 语句中呢 就是变量和sql连接的问题
      

  9.   


    --也可以这样declare @source varchar(100)
    set @source = 'aaa'select zyId,zyName,otherName,source,zucheng,useMethod,zhuzhi,bz 
    from zy_info 
    where 1 = 1 and source like '%' + @source + '%'
      

  10.   

     String where = "where 1=1";
     
      if(!fanghao.equals("")){
         where = where + " and fanghao ='"+fanghao+"' ";
      }  
      if(!xm.equals("")){
         where = where + " and xm like '%"+xm+"%'";
      }
      if(!djrq.equals("")){
         where = where + " and substring(djrq,1,10) ='"+djrq+"'";
      }
      Page pageDjda = db.selectAllRowsPage("select id,xm,fanghao,sex,zjType,zjNumber,jtzz,djrq,ysje,ssje,djje,daynumber from dengjida "+where+" and zt='1'");这句话可以执行 我想写一个类似于这个的 怎么弄呢 你的那个可以实现
      

  11.   


    一样的,按这个多定义两个变量把两外两个参数对应写进去就可以,关键是你只给了程序获取参数值的,在SQL里怎么知道那几个参数。