简体   繁体   English

向MySQL COUNT()添加限制以每天仅考虑最多3行?

[英]Adding a restriction to MySQL COUNT() to only consider a maximum of 3 rows per day?

I have the following MySQL query: 我有以下MySQL查询:

SELECT (COUNT(id) * 5) AS total FROM content WHERE author = 'newbtophp'

I also have a column called content_timestamp (which contains the UNIX timestamp of when the content was created). 我还有一列称为content_timestamp (其中包含创建内容的UNIX时间戳)。

The MySQL query is functioning fine, however I only want the COUNT(id) to consider a maximum of 3 content per day (so if the table content contains more then 3 rows of data WHERE the day (+ month + year) of the content_timestamp is the same then only consider the first 3 and disregard the rest) - this is where I believe the content_timestamp will become useful. MySQL的查询功能正常,但是我只希望COUNT(id)最多考虑3 ,每天的内容(所以如果表格content包含超过3行数据WHERE的当天(+月+年) content_timestamp是相同的,则仅考虑前3个而忽略其余的)-这是我相信content_timestamp会变得有用的地方。

However I'm unsure on how to do this? 但是我不确定如何执行此操作?

All help is greatly appreciated. 非常感谢所有帮助。

Will it work for you? 对您有用吗?

Update 更新资料

SELECT 5*SUM(IF(num_content>3,3,num_content)) 
FROM
(
SELECT DATE(content_timestamp) as dt, COUNT(id) as num_content
FROM content     
WHERE author = 'newbtophp'
GROUP BY DATE(content_timestamp)
)a

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

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