MySQL 仅选择分组的两行或更多行
我有一个包含两个字段的表: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)