在mysql中计算行数?

时间:2011-10-10 11:07:47

标签: mysql sql

我有一个返回20行的查询。

select year( ArticleDate )
from NewsData nd
inner join NewsCategories nc on nd.DirectNewsID = nc.DirectNewsID
where nd.Deleted = 0
and year( nd.ArticleDate ) = 2006
group by nd.DirectNewsID;

是否可以通过and部分中的数字20后跟年份返回单行?

结束查询不会包含and year( nd.ArticleDate ) = 2006,因此每年会返回一行计数。我仅为测试目的添加了and year( nd.ArticleDate ) = 2006

这可能吗?

4 个答案:

答案 0 :(得分:2)

这样的东西?

select year(nd.ArticleDate), COUNT(DISTINCT nd.DirectNewsID)
from NewsData nd
inner join NewsCategories nc on nd.DirectNewsID = nc.DirectNewsID
where nd.Deleted = 0
group by year(nd.ArticleDate);

修改

现在计算每年nd.DirectNewsID的明显出现次数。

答案 1 :(得分:1)

尝试:

select year( nd.ArticleDate ), count(*)
from NewsData nd
inner join NewsCategories nc on nd.DirectNewsID = nc.DirectNewsID
where nd.Deleted = 0
and year( nd.ArticleDate ) = 2006
group by year( nd.ArticleDate );

如果目标是确定DirectNewsID的数量而不是总行数,请将count(*)替换为count(distinct nd.DirectNewsID)

答案 2 :(得分:0)

您可以使用count方法。请参阅:http://www.tizag.com/mysqlTutorial/mysqlcount.php

答案 3 :(得分:0)

您只需使用count方法即可获得总行数。即

select count(*) from NewsData nd inner join NewsCategories nc  where nd.Deleted = 0, nd.DirectNewsID =  c.DirectNewsID  and year( nd.ArticleDate ) = 2006  

检查..它应该按照您的需要工作