My table structure:
+-----------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user | varchar(100) | NO | | NULL | |
| uniq_name | varchar(100) | NO | | NULL | |
| info_name | varchar(100) | NO | | NULL | |
| delay_time | int(11) | NO | | NULL | |
| track_time | timestamp | NO | | CURRENT_TIMESTAMP | |
+-----------------+--------------+------+-----+-------------------+----------------+
I have to retrieve data from table for past seven days, not summed one. Data for a specific date, for this right now I have to fired same query seven times using like operator:
SELECT COUNT(DISTINCT `user`) FROM `tracks` WHERE `track_time` LIKE '2012-11-12%';
Which is not a good way to do. I am trying to do myself, also discussed on forums , it might be done with IN operator or using date1 < date2 < ….date for past seven days. For this, i am using a function in PHP in which I am supplying a date parameter, but query will take lot of time to display result on web. Please have a look on this situation.
Best Answer
If you are doing a range of consecutive dates, there is no need to use the
IN
operator.If you use this to compute midnight seven days ago
here is what you get back:
Your query will look like this:
You also need an index to support the
WHERE
andCOUNT
Then, the query should go faster.
If you are doing a range of consecutive dates from some earlier period, simply supply the first and last date from midnight to midnight. For example, for the range
2012-11-12
to2012-11-19
:If you are doing a range of non-consecutive dates, simply structure the query using
UNION
. For example, for dates2012-11-12
,2012-11-19
,2012-11-26
:Give it a Try !!!
UPDATE 2012-11-27 13:52
If you want count of distinct users for just one date
2012-11-12
If you want count of distinct users for 7 consecutive dates
2012-11-12
-2012-11-18
(You cannot include2012-11-19
)UPDATE 2012-11-27 14:41 EDT
If you want this dynamically done as of
NOW()
, try this: