SELECT *, IF(start_date < ".$twoDaysAgo.", (posts / 172800 * 50000), (posts / (".$curTime." - start_date) * 50000)) as rating
FROM
(
SELECT t1.*, t2.*, count(t2.id) as posts
FROM topics as t1
LEFT JOIN
(
SELECT id, topic_id as tid, poster, body, post_date, poster_ip, subject
FROM messages t9
) as t2
ON t1.topic_id = t2.tid
GROUP BY t1.topic_id
) as t3
ORDER BY rating DESC, topic_id ASC
posts列提供了主题的alltime-postcounts。没关系,我想要那个。但我还想在过去两天内获得主题的帖子。换句话说,我需要在一个查询中获取主题'alltime-postcounts和2-day-postcounts。
表主题: topics http://easycaptures.com/fs/uploaded/802/8788454634.png
表消息: messages http://easycaptures.com/fs/uploaded/802/8788454634.png
答案 0 :(得分:1)
这样的事情会起作用:
SELECT count(*) AS all_time, SUM(start_date > $twoDaysAgo) AS last_2_days
MySQL会将start_date比较布尔值自动转换为整数0
或1
,然后总结那些1,有效地为您提供所需的计数。