繁体   English   中英

SQL选择计数与WHERE子句不同

[英]SQL Select Count Distinct with WHERE clause

目的:我们有一个数据库,其中包含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

我尝试了您的代码,除非您的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 /网站合并,

     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

我认为那应该使您走上正确的轨道

查询中的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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM