我有一个SQL表,如:
-----------------
id person | entity
------------------
1 | A/B
2 | A/B/C
3 | A/B/C
4 | A/B/D
5 | A/B/C
我想计算具有相同实体+具有相同母实体的人数
范例:
-----------------
entity | count
------------------
A/B | 5
A/B/C | 3
A/B/D | 1
我该如何使用SQL请求呢?
简单的答案,有一个相关的子查询,它计算相似的实体:
select distinct t1.entity, (select count(*)
from tablename t2
where t2.entity like concat(t1.entity,'%'))
from tablename t1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句