2014-07-21 91 views
0
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。沒關係,我想要。但我還想在最近2天內獲得主題'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

請添加一些數據示例以及該數據所需的結果。幫助你會容易得多。 –

+0

@JorgeCampos新增 – Wellenbrecher

+0

看來MarcB已經回答了你。看看它是否有效。 –

回答

1

像這樣的工作:

SELECT count(*) AS all_time, SUM(start_date > $twoDaysAgo) AS last_2_days 

MySQL將在起始日期比較布爾值自動轉換爲整數01,然後總結那些1,有效地給你你需要的數量。

+0

你可以把它放在我的查詢中嗎? – Wellenbrecher