当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Using select statement to find how many IPs came back today

问题描述:

I have a select statement that tries to find how many ip came back today, that also visited this month, but it's not working for some reason. What am I doing wrong?

SELECT ip, COUNT( ip ) AS matches, DATE AS tempDate

FROM ipAll

WHERE YEAR( DATE ) = '2015'

AND MONTH( DATE ) = '12'

GROUP BY ip

HAVING matches > 1

AND tempDate = '2015-12-29'

LIMIT 0 , 30

Expected Result:

For it to return one or more rows.

Actual Result:

It returned 0 rows

Note:

date is a column name, and yes, I am sure that there 2 or more ips in the database in the same year, month and day.

网友答案:

Perfectly works for me:

SELECT ip, COUNT( ip ) AS matches, DATE AS tempDate
FROM ipAll
WHERE DATE_FORMAT(DATE,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m')
GROUP BY ip HAVING matches > 1
LIMIT 0 , 30

NOTE that DATE column is a MySQL Keyword you should replace the column name with a different one

分享给朋友:
您可能感兴趣的文章:
随机阅读: