首頁 >資料庫 >mysql教程 >mysql-mySql多表查询,有中间表和外键关联

mysql-mySql多表查询,有中间表和外键关联

WBOY
WBOY原創
2016-06-06 09:41:321909瀏覽

mysql关联外键

select a.id,a.name,
sum(case when d.STATUS=0 then 1 else 0 end) as 待处理,
sum(case when d.STATUS=1 then 1 else 0 end) as 带跟进,
sum(case when d.STATUS=2 then 1 else 0 end) as 已结束,
sum(case when d.STATUS=3 then 1 else 0 end) as 已完成
from D d,A a
where a.id = d.Aid (d表的Aid和a表的id对应,但d表有多条数据的Aid=a.id)
group by a.id

select a.id,a.name as 处理人,c.name as 所属机构
from A a,B b,C c
where a.id = b.Aid and c.id = b.Cid (B表是中间表,只存放a表的ID和c表的ID)

就是把这两个sql语句写在一个里面

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn