sql语句如下:
sql = "select * from info where classid=19 and nclassid=31 and title like'%" + title + "%'and leixing like'%" + leixing + "%'and synopsis='" + synopsis + "'order by id desc";
现在我想对title(名称) leixing(类型) synopsis(索引号)任意一项进行查询,上面代码怎么修改?
效果如http://zwgk.shaoguan.gov.cn/website/govPublic/govPublicSiteAction!deptgovp_list.action?deptId=682&pagesize=15&siteOrgCode=440200

解决方案 »

  1.   


    sql = "select * from info where classid=19 and nclassid=31 and (title like'%" + title + "%' or leixing like'%" + leixing + "%'or synopsis='" + synopsis + "') order by id desc";
    and 连接改为or嘛
      

  2.   

    like 后面用 %...% 就是灾难,应该用  ...%, 去掉前面的%
      

  3.   

    是应该这样,不过前几天做的时候,这个 貌似是有bug的,所以建议小小修改下用这个:sql = "select * from info where (classid=19 and nclassid=31) and (title like'%" + title + "%' or leixing like'%" + leixing + "%'or synopsis='" + synopsis + "') order by id desc";
      

  4.   

    根据传入的值 进行sql语句拼接
      

  5.   

    用存储过程,对参数做判断 ,如下方法写。create proc 存储过程名
    @ptitle varchar(1000)=null
    @pleixing varchar(1000)=null
    @psynopsis varchar(1000)=null
    as
    select * from info where classid=19 and nclassid=31 and
    (title=isnull(@ptitle,'') or isnull((@ptitle,'')='') and
    (leixing=isnull(@pleixing,'') or isnull(@pleixing,'') ='') and
    (synopsis=isnull(@psynopsis,'') or isnull(@psynopsis,'')='')