views:

34

answers:

1

I am using this query:

SELECT p.id, count(clicks.ip)
FROM `p`
LEFT JOIN c clicks ON p.id = clicks.pid
WHERE clicks.ip = '111.222.333.444'

To select clicks from table "c", that has "pid" = "p.id". The query seems to work fine, but now I want to use that query with date ranges. The "c" table has a column "time" that uses MySQL CURRENT_TIMESTAMP data type (YYYY-MM-DD HH:MM:SS). How can I use my query with date range using that column?

I want to be able to select count(clicks.ip) from a specific day, and also group the results by hour (but this is for a different query).

A: 

Use:

   SELECT p.id, 
          COUNT(clicks.ip)
     FROM `p`
LEFT JOIN c clicks ON clicks.pid = p.id 
                  AND clicks.ip = '111.222.333.444'
                  AND clicks.time BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY)
                                      AND NOW()

I provided an example that will count clicks that occurred between this time yesterday (DATE_SUB(NOW(), INTERVAL 1 DAY)) and today (NOW()). Mind that BETWEEN is inclusive.

OMG Ponies