0

这是用于从数据库中提取数据的当前代码
$query = "SELECT username,time,UNIX_TIMESTAMP(time) as timestamp,UNIX_TIMESTAMP(active) as activestamp,IP FROM ".$table_name." WHERE success='1' ORDER BY active DESC";

这是一组数据示例(删除了所有不重要的字段)
ID | username
_01 | admin
_02 | admin
_03 | bob
_04 | jill

我想要的是每个用户出现的总次数,例如:
admin (2)
bob (1)
jill (1)

我需要在COUNT(IF(name = 'admin', 1, NULL)) AS login_totals我向您展示的原始查询中添加一些类似的内容,但我无法找出正确的代码。我的示例中的名称 admin 必须是当前用户名,所以我是否将“用户名”放在那里?

4

3 回答 3

1

试试这个:我添加了count(*) and the group by

 $query = "SELECT username,time,
UNIX_TIMESTAMP(time) as timestamp,
UNIX_TIMESTAMP(active) as 
activestamp,IP, count(*) 
FROM ".$table_name." WHERE success='1' 
group by username 
ORDER BY active DESC";

看看这个博客:http ://www.mysqlperformanceblog.com/2007/04/10/count-vs-countcol/

于 2012-10-16T20:22:21.130 回答
0

添加COUNT(username)inSELECT子句并GROUP BY username在查询末尾。

于 2012-10-16T20:24:27.613 回答
0
SELECT
   username,count(username)
   FROM ".$table_name." WHERE success='1' 
GROUP BY username
ORDER BY active DESC";
于 2012-10-16T21:01:39.263 回答