简体   繁体   中英

Query to count how many times an ip is used with different accounts

I would like a single query to count how many different customer accounts use the same IP to log in.

+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| info_id | int(11)     | NO   | PRI | NULL    | auto_increment |
| afid    | int(11)     | NO   |     | 0       |                |
| access  | date        | NO   |     | NULL    |                |
| ip      | varchar(15) | NO   |     |         |                |
+---------+-------------+------+-----+---------+----------------+

afid is the customer id. Every time they log in a row is inserted into this table. I have been trying nested selects without any luck, and anything I can think of. I'm probably over thinking this too much :)

Thanks in advance!

Try this:

SELECT COUNT(DISTINCT afid) AS afid_count
FROM yourtable
WHERE ip = '....'

To get a list of the most frequently used IPs:

SELECT
    ip,
    COUNT(DISTINCT afid) AS afid_count
FROM yourtable
GROUP BY ip
HAVING afid_count > 1
ORDER BY afid_count DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM