计算条件为“ Like”的行数

阿斯玛·乔(Asmaa Djo)

我有一个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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章