2014-12-27 48 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