我有一个带有timestamp列的表我希望得到特定月份中的时间戳(例如,9月1日到30 septemper之间的timpestamp)的值,如果月份是31天则考虑到这一点。 我使用这个查询:
SELECT users.username, users.id, count(tahminler.tahmin)as tahmins_no FROM users LEFT JOIN tahminler ON users.id = tahminler.user_id GROUP BY users.id having count(tahminler.tahmin) > 0
我可以添加where timestamp IN(dates_array)
??
date_array将是整个月的日期?
答案 0 :(得分:14)
SELECT users.username, users.id, count(tahminler.tahmin)as tahmins_no FROM users LEFT JOIN tahminler ON users.id = tahminler.user_id where year(timestamp) = 2013 and month(timestamp) = 9 GROUP BY users.id having count(tahminler.tahmin) > 0
使其可以使用您可以执行的索引
SELECT users.username, users.id, count(tahminler.tahmin)as tahmins_no
FROM users
LEFT JOIN tahminler ON users.id = tahminler.user_id
where timestamp >= '2013-09-01' and timestamp < '2013-10-01'
GROUP BY users.id
having count(tahminler.tahmin) > 0
答案 1 :(得分:4)
如果是TIMESTAMP
YEAR (TIMESTAMP) = 2013 AND MONTH (TIMESTAMP) = 9
包含在同一条款中
DATE_FORMAT(TIMESTAMP,'%Y-%m')='2013-09'
用于unix时间戳
YEAR (FROM_UNIXTIME(TIMESTAMP)) = 2013 AND MONTH(FROM_UNIXTIME(TIMESTAMP))=9
包含在同一条款中
DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP),'%Y-%m')='2013-09'
答案 2 :(得分:0)
如果我理解你的问题你可以添加:
WHERE MONTH(timestamp)=9