当前位置: 代码迷 >> Sql Server >> 求一条SQL语句,拜谢诸位大神了【MySql】
  详细解决方案

求一条SQL语句,拜谢诸位大神了【MySql】

热度:92   发布时间:2016-04-24 09:27:40.0
求一条SQL语句,拜谢各位大神了【MySql】


我的数据库是这样的。我有20条数据CommentRecID为规则ID,RoleRecID为用户,IsLike为用户是否喜欢,0为不喜欢,1为喜欢。
我的需求是这样的,查出来 所有规则中喜欢(IsLike=1)的数量最多的那条规则,和不喜欢(IsLike=0)最多的那条规则。
跪求大神!
------解决思路----------------------

select  CommentRecID,max(count(CommentRecID)) from 表 where IsLike=1  group  by CommentRecID
select  CommentRecID,max(count(CommentRecID)) from 表 where IsLike=0  group  by CommentRecID

------解决思路----------------------

 select CommentRecID
,max(sum(case when IsLike=0 then 1 else 0 end)) 
,max(sum(case when IsLike=1 then 1 else 0 end)) 
from TB 
group  by CommentRecID 

------解决思路----------------------
with a(recid,commentrecid,rolerecid,islike) as
(
select 1,1,220,1 union all
select 2,1,230,1 union all
select 3,1,240,1 union all
select 4,1,250,0 union all
select 5,1,260,0 union all
select 6,2,270,0 union all
select 7,2,280,0 union all
select 8,2,290,0)
select 
(select top 1 commentrecid from a where islike=1 group by commentrecid order by COUNT(*) desc)  as 最大co
,(select top 1 commentrecid from a where islike=0 group by commentrecid order by COUNT(*) desc)  as 最小CO
  相关解决方案