一个 union all 的all

这是个值得记念的sql语句 原本用union 怎么都查询不出正确数据 苦逼1小时原来是少了个all   呵呵强大的sql  sql

一个object 表中有tchID 和OtherTchID 均可能为空 ,让求出TchID出现次数和OtherTchID出现的次数,而后TchID和OtherTchID相同时出现次数想加   计算每一个ID出现的总次数。 io

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语句的基础练习  加油了!!! object

相关文章
相关标签/搜索