0

I want to get data for the dates between 2015-05-01 and 2015-06-01 using SQL. Please help me with the query.

The query I used is:

select *,count(id) as multiple_visitors 
    from table1 
    where id=123 
        and (date(server_time) between (CURDATE() - INTERVAL 31 DAY) AND CURDATE()) 
    group by user_id having count(id)>1 
Jason
  • 15,017
  • 23
  • 85
  • 116

2 Answers2

1

You can do this with month() and year():

where month(server_time) = month(curdate() - interval 1 month) and
      year(server_time) = year(curdate() - interval 1 month)

However, I recommend a slightly more complex expression:

where server_time >= date_sub(date_sub(curdate(), interval - day(curdate()) + 1 day), interval 1 month) and
      server_time < date_sub(curdate(), interval - day(curdate()) + 1 day)

The advantage is that there are no functions on server_time, so the database engine can use an index, if appropriate.

As a note: the expression date_sub(curdate(), interval - day(curdate()) + 1 day) gets midnight on the first day of the month.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Try using "WHERE" with MONTH(date).

Like this:

  SELECT * FROM Table
  WHERE MONTH(date) = 1
Justin W. Johns
  • 119
  • 1
  • 8