简体   繁体   English

如何计算SQL数据库中的记录?

[英]How to count records in sql database?

I know how to get the number of rows using mysql_num_rows. 我知道如何使用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. 我想做的是计算每个IP地址,因此我可以计算数据库中有多少个不同的IP地址。 if that makes sense lol. 如果这有意义的话。 cause there is just over 1,000 records. 因为这里有超过1,000条记录。 so let me show you a quick example. 让我给你看一个简单的例子。

say i have these ip addresses. 说我有这些IP地址。

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. 我希望它算每个1。 and print 3 or whatever ammount i have in my database not count all which is what mysql_num_rows does and prints 7. 并打印3或我数据库中的任何数量都不计算mysql_num_rows所做的全部并打印7。

sorry if the title isn't very specific. 抱歉,如果标题不是很具体。

一起使用非distinctcount功能:

SELECT COUNT(distinct ip_address) FROM some_table;

Use DISTINCT to count once. 使用DISTINCT计数一次。 Not involving the duplicates. 不涉及重复项。 Try this query: 试试这个查询:

SELECT COUNT(DISTINCT IP) 
FROM table_name;

使用MySQL Distinct子句

SELECT DISTINCT IpAddress FROM MyTable
SELECT ip , COUNT(ip)
FROM table_name GROUP BY ip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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