2011-06-03 99 views
2

我怎麼能採取MySQL查詢像這樣:如何限制mysql的查詢,並限制它的時間戳?

SELECT id,title,timestamp,upvotes,downvotes,views FROM {$table} ORDER BY count DESC LIMIT $start_from, 20 

並基於timestamp(這是一個標準的MySQL時間戳)

讓我的MySQL不僅LIMIT $start_from, 20但除了...

限制由:

如果今天 製成,如果這是今天和一個星期前 之間進行,如果這是今天和莫之間進行nth ago

有沒有想法?

回答

8
SELECT id, title, timestamp, upvotes, downvotes, views 
FROM {$table} 
WHERE timestamp >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH) 
ORDER BY count 
DESC LIMIT $start_from, 20 

MySQL Reference Manual DATE_SUB

1

添加where子句以檢查所需範圍的時間戳。

2
SELECT id,title,timestamp,upvotes,downvotes,views 
FROM {$table} 
where timestamp >= now() - interval 1 week 

SELECT id,title,timestamp,upvotes,downvotes,views 
FROM {$table} 
where timestamp >= now() - interval 1 month