SQL选择Count WHinct with WHERE子句

时间:2014-01-14 12:34:06

标签: sql

目标:我们有一个包含UserID和SiteID的数据库,如果他们的userID / SiteID匹配,那么他们可以看到详细信息。

思想:我尝试过使用Count(Distinct ...)来查看它是否存在。然后,如果它返回的值不为零,我知道他们可以看到详细信息。

这是最好的方法吗?

问题:尝试使用这种方式我无法使WHERE子句起作用。

错误:错误因我演奏而异,但基本上说我不能有WHERE子句,这是正确的吗?

代码:

Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("mySQL").ToString()
                Using connection = New SqlConnection(ConnectionString)
                    Using command As New SqlCommand("Select Count(Distinct(UserID)) FROM tblTable where UserID= '" & Session.Item("UserID").ToString & "'", connection)

                        connection.Open()
                        Dim result = command.ExecuteScalar()

                        lblResult.Text = result 

                        connection.Close()
                    End Using
                End Using

2 个答案:

答案 0 :(得分:1)

我尝试了您的代码,除非您的Session.Item(“UserId”)未正确设置,否则我看不出您遇到问题的位置。我没有会话,所以我在控制台应用程序中即兴创作。我试图找出你究竟想要做什么,因为Count(Distinct(UserId))将始终返回1.因为你要求一个特定的userId,然后要求区分它然后计算它。所以1个用户返回100次,不同的是给你列表中唯一的用户,所以1返回。

 Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ToString()
    Using connection = New SqlConnection(ConnectionString)

        Using command As New SqlCommand()
            Dim userId As String = "3"
            command.Connection = connection
            command.CommandText = "Select Count(Distinct(UserId)) FROM tblTable _
                                      where UserId= '" & userId & "'"

            connection.Open()
            Dim result = command.ExecuteScalar()

            Console.WriteLine(result)

            connection.Close()
        End Using
    End Using

如果您只是想查看用户是否存在,请尝试以下代码。

     Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ToString()
    Using connection = New SqlConnection(ConnectionString)

        Using command As New SqlCommand()
            Dim userId As String = "3"
            command.Connection = connection
            command.CommandText = "Select Count(UserId) FROM tblTable _
                                      where UserId= '" & userId & "'"

            connection.Open()
            Dim result = command.ExecuteScalar()

            Console.WriteLine(result)

            connection.Close()
        End Using
    End Using

这将告诉您表中包含userIds的行数

如果你想看到UserID / Site Comination的计数是另一个故事

     Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ToString()
    Using connection = New SqlConnection(ConnectionString)

        Using command As New SqlCommand()
            Dim userId As String = "3"
            command.Connection = connection
            command.CommandText = "Select UserID, Count(SiteId) FROM tblTable where _   
                          UserId= '" & userId & "'" & " Group By UserId"

            connection.Open()
            Dim dataReader = command.ExecuteReader()
            Do While dataReader.Read()
                Console.WriteLine( _
                    vbTab & "UserId: {0}" & vbTab & "SiteID Count: {1}", _
                 dataReader(0), dataReader(1))
            Loop
            connection.Close()
        End Using
    End Using

我认为应该让你走上正确的轨道

答案 1 :(得分:0)

查询中的where子句没有问题:

select count(distinct userid)
from tblTable
where UserID= '" & Session.Item("UserID").ToString & "'";

在某些SQL引擎中,可能不允许userid周围的附加括号。

在任何情况下,这不是确定行是否存在的最佳方法,因为它必须进行大量处理。而是检查此查询返回的行数(如果引擎支持limit):

select 1
from tblTable
where UserID= '" & Session.Item("UserID").ToString & "'"
limit 1

或者(在SQL Server / Sybase中):

select top 1 1
from tblTable
where UserID= '" & Session.Item("UserID").ToString & "'"
limit 1;