这是个值得纪念的sql语句 本来用union 怎么都查询不出正确数据 苦逼1小时原来是少了个all 呵呵强大的sql
一个object 表中有tchID 和OtherTchID 都可能为空 ,让求出TchID出现次数和OtherTchID出现的次数,然后TchID和OtherTchID相同时出现次数想加 计算每个ID出现的总次数。
select a.TchID,sum(a.num) from(
select TchID, count(TchID) as num from subject group by TchID
union all
select OtherTchID as TchID, count(TchID) as num from subject group by OtherTchID
) a group by a.TchID
sql语句能解决的问题我觉得还是在sql语句中解决最好,解决后就像吃了口肉,恩 以后加强sql语句的基础练习 加油了!!!
来源:oschina
链接:https://my.oschina.net/u/269285/blog/91181