开发者

MySQL select only two or more rows grouped

I have a table containing two fields: ID and USERNAME. How to my make the SELECT query that will select only rows with minimal two rows grouped?

This is what inside the MySQL table:

ID| Username

1 | peter

2 | jack

3 | peter

4 | ann

5 | peter

6 | jack

I want to select in result like this:

peter (3)

jack (2)

Username 'ann' will be ignored in selection cause it's only one row.

Added: Cannot implement it to my situation. Ok, I will show what I exactly want to do, something like this: (SELECT ip AS ip1 FROM ip_denylist) UNION (SELECT COUNT(user) AS sometotal FROM entrances WHERE ip = ip1 AND HAVING COUNT(sometotal) > 1) GROUP B开发者_JAVA百科Y user.

But it's not working! Could you show how it must be?

The idea of this select is first: to select each ip address from the ip_denylist table, 2nd: select grouped by user rows from the table entrances that have same ip addresses.


SELECT UserName, COUNT(UserName) AS sometotal FROM table
GROUP BY UserName HAVING COUNT(UserName) > 1


select username,count(*) as numb 
from table 
group by username 
having numb > 1
order by numb desc
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜