sadrac
sadrac

Reputation: 37

How to get a field value and all related elements by using that field value in the same query?

I have a table that has these fields:

clickId, ip, date

I need to get all dates in which an user (with an IP like 192.168.0.55) did clicks, and for every date how many clicks he did.

How can I do this with one single MySql query?

Upvotes: 0

Views: 27

Answers (2)

John Ruddell
John Ruddell

Reputation: 25862

you can just group by the ip and date. if it is a datetime you need to group by DATE(date)

SELECT  ip, DATE(date), COUNT(*) as num_clicks
FROM your_table
GROUP BY ip, DATE(date)

this will be for all ip addresses by a specific date. you can also specify a specific ip address in a WHERE clause aka. WHERE ip = "192.168.0.55"

Upvotes: 1

Barmar
Barmar

Reputation: 781726

Use GROUP BY along with an aggregation function.

SELECT date, COUNT(*) AS click_count
FROM YourTable
WHERE IP = '192.168.0.55'
GROUP BY date

Upvotes: 0

Related Questions