简体   繁体   中英

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 BY 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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