一个页面多种可能查询,怎么实现呢?只有一个按钮,可是查询条件组合可能有多个,例如按@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();
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();
sql知道,这么多可能的条件查询按if判断么?
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 + "%'";这样判断?
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()+"%' ";
}
......
......
......
set @sql = @sql + ' and code = ''' + @code + '''' if coalesce(@customer, '') != ''
set @sql = @sql + ' and customer = ''' + @customer + ''''...
print(@sql) --调试时用
exec(@sql)