select content,sum(case when f_isread=1 then sender else null end )+sum(case when f_isread=1 then receiver else null end)未读数
from table
group by content
select [对象], sum([未读数]) from
(
select receiver as [对象],1 as [未读数] from [聊天信息表] where sender='我的名称' and isread=0
union all
select sender as [对象],1 as [未读数] from [聊天信息表] where receiver='我的名称' and isread=0
) t group by [对象]
表数据贴一点来看看,如果有什么特殊规则也说清楚,大概写法:
select id,sum(case whe isread=0 then sender else null end )+sum(case whe isread=0 then receiver else null end)未读数
from table
group by id
我假设isread=0的为未读