查找最近n个帖子中的用户帖子数

时间:2014-11-10 22:38:09

标签: php mysql

我试图找到最有效的方法来查找用户在最后n行表格中的帖子数量。我已尝试

$db->query_first("SELECT COUNT(*) AS
user_recent_posts FROM " . TABLE_PREFIX . "post WHERE userid = $userid
AND threadid = '" . $threadinfo['threadid'] . "' AND visible = '1'
ORDER BY dateline DESC LIMIT 10" );

我试图在帖子的最后10个帖子中获取用户帖子..但这显然会返回用户的最后10个帖子,无论他们是否在最后10个帖子中

1 个答案:

答案 0 :(得分:1)

使用subselect首先获取 n 行,然后计算用户的行:

$db->query_first("
  SELECT COUNT(b.*) AS user_recent_posts 
  FROM (
    SELECT a.* 
    FROM " . TABLE_PREFIX . "post a
    AND a.threadid = '" . $threadinfo['threadid'] . "' AND a.visible = '1'
    ORDER BY a.dateline DESC LIMIT 10) b
  WHERE b.userid = $userid" );