一个页面多种可能查询,怎么实现呢?只有一个按钮,可是查询条件组合可能有多个,例如按@code和@customer,或@email和@type,组合条件很多,这样情况下该怎么判断?
        string @code = coupon_code.Text.Trim();
        string @customer = customers_name.Text.Trim();
        string @email = customers_email.Text.Trim();
        string @sdate = create_date.Text.Trim();
        string @edate = credate.Text.Trim();
        string @type = ctype.Text.Trim();
        string @status = cstatus.Text.Trim();

解决方案 »

  1.   

    条件从何而来,根据条件组合不同的sql就是了,到底用那几个组合,这是你定的
      

  2.   

    select * from table1 where (code=@code and customer=@customer) or (email=@email and type=@type)这样
      

  3.   


    sql知道,这么多可能的条件查询按if判断么?
      

  4.   

    sql知道,这么多可能的条件查询按if判断么??
      

  5.   

    select * from table1 where (code=@code or @code is null) and (email=@email or @email is null)默认为空就行,有值的话就并列判断;无值的话始终为真,并判断其他的条件是否满足
      

  6.   

    请Google关键字:Specification利用规约实现复杂查询条件,表达会相对简洁许多。
      

  7.   


    if (@code.Length != 0 && [email protected]  && [email protected]  && [email protected]  && [email protected] && [email protected] && [email protected])
            {
                string sql = @"select * from a,b,c
                where a.type=b.type and a.id=c.id
                and a.code like '%" + @code + "%'";这样判断?
      

  8.   


    string strSQL = "SELECT * FROM TABLE ";
    strSQL = "WHERE 1 = 1 ";if (coupon_code.Text.Trim() != "")
    {
        strSQL += "AND _code LIKE '%"+coupon_code.Text.Trim()+"%' ";
    }
    if (customers_name.Text.Trim() != "")
    {
        strSQL += "AND _custorers LIKE '%"+customers_name.Text.Trim()+"%' ";
    }
    ......
    ......
    ......
      

  9.   

    like %...% 的写法尽量避免,性能较差,不能利用索引
      

  10.   

    5楼已经给出了正确答案。还有一种方法是用动态sql:declare @sql varchar(2000)set @sql = 'select * from table1 where 1=1 'if coalesce(@code, '') != ''
       set @sql = @sql + ' and code = ''' + @code + '''' if coalesce(@customer, '') != ''
       set @sql = @sql + ' and customer = ''' + @customer + ''''...
    print(@sql) --调试时用 
    exec(@sql)
      

  11.   

    输入框前面加checkbox,根据选中情况拼凑sql