2011-05-05 76 views
0
SqlCommand sqlcmd = new SqlCommand("SELECT Username, TimesUsed FROM UserInfo " + 
      "WHERE (Category IN (" + query + ")) " + "ORDER BY TimesUsed DESC", conn); 

其中query ='css','php','html'代表例如通過一組中的大多數標籤進行訂購

我想以這樣一種方式排列它們,使查詢中具有最多標記的用戶位於頂部。

當前它們根據它們使用該集合的特定標籤的次數來排序。

有什麼想法?在此先感謝:)

回答

1

所以,如果我明白,你需要使用的所有標籤的總數,並TimesUsed與特定標籤。如果這是正確的,那麼你可以SUM(TimesUsed)GROUP BY Username

SqlCommand sqlcmd = new SqlCommand("SELECT Username, SUM(TimesUsed) AS TotalUsed FROM UserInfo " + 
     "WHERE (Category IN (" + query + ")) " + "GROUP BY Username ORDER BY SUM(TimesUsed) DESC", conn); 
     sqlcmd.Parameters.AddWithValue("@c", query); 

你完整的SQL:

SELECT 
    Username, 
    SUM(TimesUsed) AS TotalUsed 
FROM UserInfo 
WHERE Category IN ('php','css','sql','etc') 
GROUP BY Username 
ORDER BY SUM(TimesUsed) DESC