0

我在经典 ASP 中使用的 MS SQL 语句遇到了一些问题。当我在 Microsoft SQL SMS 中使用该语句时 - 我得到了正确的答案 (4) 但在 ASP 中我只得到了结果 2..

我想要的结果是有多少不同的艺术家拥有一个或多个已发布的项目。

我使用的声明 -

SELECT Count(*) AS CountArtists 
FROM Tekster 
WHERE Published='True' 
GROUP BY Artist  

数据 - 表格(文本)

Artist    -   Published  
Person1       yes  
Person1       no  
Person1       yes  
Person2       yes  
Person3       yes  
Person3       no  
Person3       yes  
Person4       no  
Person4       no  
Person4       no  
Person5       no  
Person5       yes  
Person6       no  
4

5 回答 5

0

确保两个查询的连接字符串相同。您访问的数据库可能与您认为的不同。

另外,请注意您的查询正在检查True,但您的数据显示yes/no。它是哪一个?

于 2012-08-14T16:20:39.863 回答
0

当数据包含“是”和“否”时,为什么要说“已发布 = 'true'”。

尝试使用已发布 = 'yes' 运行查询。

此外,如果您想要值 4,那么您应该执行“count(distinct artist)”而不是 count(*)。

于 2012-08-14T16:21:22.697 回答
0

如果您修复该where子句,您的原始查询将为您提供每个艺术家的表中所有行的计数 where publishedis yes|true。您可能会得到一些零值。

要获得您想要的结果并回答“有多少不同的艺术家拥有一个或多个已发布项目?”的问题,您需要执行以下操作:

select count(*)
from ( select artist
       from Tekster
       where published = 'yes'
       group by artist
       having count(*) > 1
     ) published_artist pa

虚拟表(子查询)为您提供一组已发布的艺术家。外部查询为您提供已发布艺术家的数量。

于 2012-08-14T16:22:47.283 回答
0

从 Tekster WHERE Published='True' 选择 COUNT(DISTINCT Artist) 作为 CountArtists

于 2012-08-14T16:23:21.573 回答
0

如果已发布字段数据类型为位,则使用已发布 = 1

于 2012-08-14T16:27:46.453 回答