5

我有一个带有时间戳列的表,如果该月是 31 天,我想获取特定月份的时间戳(例如 9 月 1 日到 9 月 30 日之间的时间戳)考虑的值。我使用这个查询:

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 将是整个月的日期?

4

3 回答 3

17
SELECT users.username, users.id, count(tahminler.tahmin) as tahmins_no
来自用户
LEFT JOIN tahminler ON users.id = tahminler.user_id  
其中年(时间戳)= 2013 和月(时间戳)= 9
GROUP BY users.id
计数(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
于 2013-09-20T13:21:36.287 回答
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'
于 2013-09-20T13:32:10.943 回答
0

如果我正确理解您的问题,您可以添加:

WHERE MONTH(timestamp)=9
于 2013-09-20T13:24:47.010 回答