views:

95

answers:

2

This SQL statement

SELECT `ip`, `when` FROM `metrics` WHERE `vidID` = '1' GROUP BY DATE('when')

returns one result, even though multiple are present in the table with different dates. I've tried using DATE_FORMAT as well. Am I doing something stupid?

When is a timestamp column with full timestamp, including hours, minutes and seconds. I'm trying to just group by results by day/month/year.

+4  A: 

Looks like you're grouping by the constant string 'when' instead of the field when.

Use this instead:

GROUP BY DATE(`when`)

Sounds like you want to count the IP addresses for a given date:

SELECT COUNT(`ip`) AddressCount, `when`
FROM `metrics` 
WHERE `vidID` = '1' 
GROUP BY DATE(`when`)
Welbog
+1 just beat me to it
Eric Petroelje
thanks for the reply, Welbog... goes to a syntax error on that
WiseDonkey
@WiseDonkey: Care to give the details of the syntax error?
Welbog
you need to use the FROM_UNIXTIME() function on this field first, as you are saying it's in a timestamp.
duckyflip
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'when) LIMIT 0, 30' at line 1... that's with the only when now being DATE(when)
WiseDonkey
@WiseDonkey: Put `when` in backticks. It's a reserved word. I should have thought of that.
Welbog
works, thanks Welbog! Much apprecaited.
WiseDonkey
A: 

Usually, GROUP BY is used in conjunction with some aggregate function (like SUM, say) to compute the result set by one or more columns. What are you trying to accomplish here? Do you mean to sort, or just get a collapsed list of which IPs have records on which dates?

Schamp
latter. I'm just trying to get number total number of records for a date for now, and then total number of unique ips in another query later.
WiseDonkey