user4435149
user4435149

Reputation:

How to count records in sql database?

I know how to get the number of rows using mysql_num_rows. What i want to do is count each ip address, so i can count how many diffrent ip addresses are in my db. if that makes sense lol. cause there is just over 1,000 records. so let me show you a quick example.

say i have these ip addresses.

127.0.0.1
127.0.0.1
127.0.0.1
127.0.0.2
127.0.0.2
127.0.0.3
127.0.0.3

i want it to count 1 of each. and print 3 or whatever ammount i have in my database not count all which is what mysql_num_rows does and prints 7.

sorry if the title isn't very specific.

Upvotes: 1

Views: 130

Answers (4)

Akash
Akash

Reputation: 114

SELECT ip , COUNT(ip)
FROM table_name GROUP BY ip

Upvotes: 0

Hanlet Escaño
Hanlet Escaño

Reputation: 17380

Use the MySQL Distinct Clause

SELECT DISTINCT IpAddress FROM MyTable

Upvotes: 0

Yodism
Yodism

Reputation: 247

Use DISTINCT to count once. Not involving the duplicates. Try this query:

SELECT COUNT(DISTINCT IP) 
FROM table_name;

Upvotes: 2

Levi Morrison
Levi Morrison

Reputation: 19552

Use the distinct and count features together:

SELECT COUNT(distinct ip_address) FROM some_table;

Upvotes: 6

Related Questions