MySQL 仅选择分组的两行或更多行

发布于 2024-10-19 23:32:24 字数 645 浏览 2 评论 0原文

我有一个包含两个字段的表:ID 和用户名。如何进行 SELECT 查询以仅选择最少两行分组的行?

这是 MySQL 表中的内容:

ID|用户名

1 |彼得
2 |杰克
3 |彼得
4 |安
5 |彼得
6 | jack

我想在结果中选择这样的:

peter (3)
jack (2)

用户名​​“ann”在选择中将被忽略,因为它只有一行。

补充:无法将其应用于我的情况。好的,我将展示我到底想要做什么,如下所示: (SELECT ip AS ip1 FROM ip_denylist) UNION (SELECT COUNT(user) AS sometotal FROM条目 WHERE ip = ip1 AND HAVING COUNT(sometotal) > 1) GROUP由用户。

但这不起作用!你能证明它一定是怎样的吗?

这个选择的想法是第一个:ip_denylist表中选择每个IP地址,第二个:选择按用户分组具有相同 IP 地址的表 入口 行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2024-10-26 23:32:24
SELECT UserName, COUNT(UserName) AS sometotal FROM table
GROUP BY UserName HAVING COUNT(UserName) > 1
SELECT UserName, COUNT(UserName) AS sometotal FROM table
GROUP BY UserName HAVING COUNT(UserName) > 1
愁杀 2024-10-26 23:32:24
select username,count(*) as numb 
from table 
group by username 
having numb > 1
order by numb desc
select username,count(*) as numb 
from table 
group by username 
having numb > 1
order by numb desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文