2014-12-27 59 views
0

我是一个asp.net网页开发人员我正在开发一个网站,其中我需要高级排序,如www.olx.com这是一个分类网站,但问题是我有超过25个类别,我正在使用SQL服务器&有我的表分解成部分。SQL Server高级整理

现在,因为我有很多类别,所以当我对它们进行排序时,例如,如果我搜索三星,但同时我想按价格排序搜索数据(高到低或从低到高)&也同时我想筛选具有描述我现在就需要使用,如果公司进行查询的100的,但有没有更方便的解决这个问题

目前我使用这个查询数据:

sql = "SELECT * FROM users_table INNER JOIN response_table ON users_table.ID = response_table.ID LEFT OUTER JOIN miscellaneous_table ON users_table.ID = miscellaneous_table.ID LEFT OUTER JOIN response_table2 ON users_table.ID = response_table2.ID"; 

sql = sql + " where response_table.sub_category='" + incat + "'"; 

if (Request["search"] != "" && Request["search"] != null) 
{ 
    var search = Request["search"].Trim(); 
    string[] querynew = search.Split(' '); 
    var searchquery = " and "; 

    foreach (string word in querynew) 
    { 
     searchquery += " users_table.adtitle LIKE '%" + word + "%' OR "; 
    } 

    sql = sql + searchquery.Remove(searchquery.Length - 4); 
} 

if (Request["min"] != "" && Request["min"] != null && Request["max"] != null && Request["max"] != "") 
{ 
    sql = sql + " and (CAST(response_table.price AS Float)) between " + Request["min"].Trim() + " AND " + Request["max"].Trim(); 
} 

谢谢

+0

什么是您的DBMS? SQL Server? – dario 2014-12-27 18:42:47

+0

是的,我正在使用sql服务器 – user4286898 2014-12-27 18:43:27

+1

唉! sql注入漏洞!它烧伤我们! – 2014-12-27 20:03:19

回答

0

Yo ü可以用

SELECT ... 
FROM ... 
WHERE (@col1 = 0 OR col1 = @col1) 
AND (@col2 = '' OR col2 = @col2) 
AND (@col3 = 0 OR col3 = @col3) 

所以使用此短手WHERE子句

在假设你的过滤器是@ COL1,@ col2上,@ COL3以上where子句中,如果用户只为COL2电源滤波器然后就通过0进入col1 & col2,它将不会过滤这些。

+0

感谢您的回答 – user4286898 2014-12-28 18:51:56

+0

它对您有帮助吗? – 2014-12-30 14:26:48

+0

不,我找到了更好的解决方案 – user4286898 2015-01-01 15:46:04