从 MySQL 查询中选择所有 id 但有限制?

发布于 2024-12-09 06:42:17 字数 808 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2024-12-16 06:42:17

当然必须存在更有效的解决方案(JOIN),但以下内容是可读的:

SELECT id, content_timestamp
FROM content c
WHERE author = 'newbtophp'
AND (SELECT COUNT(*)
     FROM content
     WHERE DATE(FROM_UNIXTIME(content_timestamp)) = DATE(FROM_UNIXTIME(c.content_timestamp))
     AND content_timestamp < c.content_timestamp
    ) < 3
ORDER BY content_timestamp

There certainly must exist a more efficient solution (JOIN), but the following is readable:

SELECT id, content_timestamp
FROM content c
WHERE author = 'newbtophp'
AND (SELECT COUNT(*)
     FROM content
     WHERE DATE(FROM_UNIXTIME(content_timestamp)) = DATE(FROM_UNIXTIME(c.content_timestamp))
     AND content_timestamp < c.content_timestamp
    ) < 3
ORDER BY content_timestamp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文