7

I have a table with timestamp column i want to get the values where the timestamp in specific month (for example where the timpestamp between 1 september and 30 septemper) taking in considration if the month is 31 day. I use this query:

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

Can i add where timestamp IN(dates_array)??

date_array will be the dates of the whole month??

Frederic Close
  • 9,389
  • 6
  • 56
  • 67
Basel
  • 1,305
  • 7
  • 25
  • 34
  • 1
    You should use `BETWEEN` clause, if you want to check an interval. Check out here: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between – Minoru Sep 20 '13 at 13:29

3 Answers3

19
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

To make it work with indexes you can do

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
juergen d
  • 201,996
  • 37
  • 293
  • 362
4

In case of TIMESTAMP

YEAR (TIMESTAMP) = 2013 AND MONTH (TIMESTAMP) = 9

To include in the same clause

DATE_FORMAT(TIMESTAMP,'%Y-%m')='2013-09'

For unix time stamp

YEAR (FROM_UNIXTIME(TIMESTAMP)) = 2013 AND MONTH(FROM_UNIXTIME(TIMESTAMP))=9

To include in the same clause

DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP),'%Y-%m')='2013-09'
Srini V
  • 11,045
  • 14
  • 66
  • 89
0

If I understand your question correctly you can just add:

WHERE MONTH(timestamp)=9
Dan Bellandi
  • 526
  • 3
  • 5