-1

我的 Access 数据库数据如下所示:

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

我想计算likes列中每个单独的值(即001,002等)出现在表中的次数。

我使用了这段代码:

 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
4

1 回答 1

0

也许你可以用代码而不是用sql来解决它。我试过 VB.NET 但这不是我的第一语言,我使用了一个测试数据表

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
于 2013-10-16T08:21:51.653 回答