一個 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

相關文章
相關標籤/搜索