我正在尝试创建一个表,该表输出用户列表以及他们登录的次数。
每次有人登录时都会在表中创建一个新行,因此一个用户有多个行。
现在,我正在尝试使用以下语句来提取数据:
SELECT * FROM logins GROUP BY user ORDER BY timestamp DESC
哪个工作正常,但现在我的 HTML 表中有一个列应该显示用户登录的次数。我如何计算每个组中的行数?
试试这个:
SELECT *, COUNT(*) AS count FROM logins GROUP BY user ORDER BY timestamp DESC
SELECT user, count(timestamp) as login_count
FROM logins
GROUP BY user
ORDER BY login_count DESC
获取按登录次数排序的用户
使用 count()... 试试这个
SELECT *, COUNT(*) as totalcounts FROM logins GROUP BY user ORDER BY timestamp DESC
COUNT() 函数返回与指定条件匹配的行数。
SELECT user,count(*) as NoOfTimes
FROM logins
GROUP BY user
ORDER BY timestamp DESC
使用COUNT
:
SELECT user, COUNT(user) 'How Many Times he logged in'
FROM logins
GROUP BY user
ORDER BY timestamp DESC