从 MySQL 查询中选择所有 id 但有限制?
我有以下 MySQL 查询:
SELECT SUM(IF(num_content>3,3,num_content))
FROM
(
SELECT DATE(FROM_UNIXTIME(content_timestamp)) as dt, COUNT(id) as num_content
FROM content
WHERE author = 'newbtophp'
GROUP BY DATE(FROM_UNIXTIME(content_timestamp))
)a
它返回内容计数(来自特定内容作者),但具有以下限制 =>它每天最多只考虑 3 行数据(使用 content_timestamp
)。
它工作正常,但是现在我想返回所有属于以下范围的ID(执行查询
,然后使用mysql_fetch_assoc
执行while循环
)对于该限制,我尝试了以下查询:
SELECT id
FROM
(
SELECT DATE(FROM_UNIXTIME(content_timestamp)) as dt, COUNT(id) as num_content, id FROM content
WHERE author = 'newbtophp'
GROUP BY DATE(FROM_UNIXTIME(content_timestamp))
)a
但没有运气(因为它没有返回带有限制的 id)。
对我来说,主要问题是我不确定如何在返回 id 的同时整合限制。
我感谢所有的回应。
I have the following MySQL query:
SELECT SUM(IF(num_content>3,3,num_content))
FROM
(
SELECT DATE(FROM_UNIXTIME(content_timestamp)) as dt, COUNT(id) as num_content
FROM content
WHERE author = 'newbtophp'
GROUP BY DATE(FROM_UNIXTIME(content_timestamp))
)a
Which returns the a count of content (from a specific content author) but with the following restriction => it will only consider a maximum of 3 rows of data per day (using the content_timestamp
).
It is working fine, however now I'm wanting to return all ids (do a query
then a while loop
with mysql_fetch_assoc
) which fall under that restriction, I've tried the following query:
SELECT id
FROM
(
SELECT DATE(FROM_UNIXTIME(content_timestamp)) as dt, COUNT(id) as num_content, id FROM content
WHERE author = 'newbtophp'
GROUP BY DATE(FROM_UNIXTIME(content_timestamp))
)a
But no luck (as its not returning the ids with the restriction).
The main issue for me is I'm not sure how to integrate the restriction whilst returning the ids.
I appreciate all responses.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然必须存在更有效的解决方案(JOIN),但以下内容是可读的:
There certainly must exist a more efficient solution (JOIN), but the following is readable: