2013-10-16 70 views
-1

我的Access數據庫中的數據是這樣的:如何計算特定的子多少次出現在數據庫

srno  likes 
user 1  001,002,003 
user 2  001,004,005,007 
user 2  001,007,009,001 
etc 

我要計算多少次在likes列中的每個單獨的值,即001002,等等,出現在表中。

我用這個代碼:

cnn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data\Acetech.mdb")) 
     cnn.Open() 
     msg = "SELECT COUNT(001) FROM (productfeedback) WHERE three like '001' OR second like '001' " 

     cmd = New OleDbCommand(msg, cnn) 
     str = cmd.ExecuteScalar() 

     cnn.Close() 

它應該顯示001 4

If str Is DBNull.Value Then 
     Label12.Text = "0" 
    Else 
     Label12.Text = Format(CStr(TextBox2.Text)) & Convert.ToInt32(str) 
    End If 
+0

如何將喜歡的東西存儲在數據庫中??作爲一個字符串? –

+0

like is in string format – megha

+0

please help ..... – megha

回答

0

也許你可以通過SQL代碼解決它,而不是。我試過VB.NET但這不是我的第一語言,我用了一個測試Datatable

Dim dt As New DataTable() 

dt.Columns.Add("srno", GetType(String)) 
dt.Columns.Add("likes", GetType(String)) 

dt.Rows.Add("User 1", "001,002,003") 
dt.Rows.Add("User 2", "001,004,005,007") 
dt.Rows.Add("User 3", "001,007,009,001") 

Dim q = From l In dt.AsEnumerable() _ 
      Select l 

Dim likes As New List(Of String)() 
For Each row In q 
    likes.AddRange(row("likes").Split(",")) 
Next 

Dim likesGroupedBy = likes.GroupBy(Function(item) item).[Select](Function(group) New With { _ 
group.Key, _ 
Key .Count = group.Count() _ 
}) 

Dim ret As String = String.Empty 
For Each row In likesGroupedBy 
    ret += String.Format("key:{0}, count:{1}" + Environment.NewLine, row.Key, row.Count) 
Next 
相關問題