【问题标题】:MySQL statement that returns only rows with exact equal amounts per IDMySQL 语句仅返回每个 ID 具有完全相同数量的行
【发布时间】:2020-12-04 20:34:36
【问题描述】:

t1

id | provider
_________
1  | bob
1  | ted

2  | bob
2  | bob
2  | ted
2  | ted

3  | bob
3  | bob
3  | ted
3  | ted
3  | joe
3  | joe

4  | bob
4  | bob
4  | ted
4  | joe
4  | joe

返回 id 的 MySQL 语句,其中所有名称在每个 ID 中显示的数量相同。对于 ID 4,名称“ted”仅出现一次,而其他名称出现两次,因此将从结果中排除。

id
__
1
2
3

【问题讨论】:

  • 你的主键是什么?
  • pid,对不起,我把它放在了桌子外面。
  • 提供了一个编辑按钮

标签: mysql sql


【解决方案1】:

使用两个级别的聚合:

select id
from (select id, name, count(*) as cnt
      from t1
      group by id, name
     ) ni
group by id
having min(cnt) = max(cnt);

【讨论】:

    猜你喜欢
    • 2020-11-06
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多