最基本的主外键连接,注意写明属主。见下面所示:select a.代码, a.姓名,a.类别,b.金额,c.出勤天数,d.缺勤天数 from 资料表 a,类别表 b,出勤表 c,缺勤表 d where a.代码 = c.代码 and a.代码 = d.代码 and a.类别 = b.类别
SELECT 资料表.代码 资料表.姓名 资料表.类别 类别表.金额 出勤表.出勤天数 缺勤表.缺勤天数 FROM 资料表 INNER JOIN 类别表 ON 类别表.类别 = 资料表.类别 INNER JOIN 出勤表 ON 出勤表.代码 = 资料表.代码 INNER JOIN 缺勤表.代码 = 资料表.代码
不行呀 记录不完整。 是不是要用 full join 等其它的连接,,才能达到上述效果
不过我把你的语句改了一下, 就行了.SELECT 资料表.代码 资料表.姓名 资料表.类别 类别表.金额 出勤表.出勤天数 缺勤表.缺勤天数 FROM 资料表 FULL JOIN 类别表 ON 类别表.类别 = 资料表.类别 FULL JOIN 出勤表 ON 出勤表.代码 = 资料表.代码 FULL JOIN 缺勤表.代码 = 资料表.代码谢谢你,,给分!
select * ,金额=(select sum(金额) from 类别表 where 类别=a.类别) ,出勤天数=isnull((select sum(出勤天数) from 出勤表 where 代码=a.代码),0) ,缺勤天数=isnull((select sum(缺勤天数) from 缺勤表 where 代码=a.代码),0) from 资料表 a
select * ,金额=(select sum(金额) from 类别表 where 类别=a.类别) ,出勤天数=isnull((select sum(出勤天数) from 出勤表 where 代码=a.代码),0) ,缺勤天数=isnull((select sum(缺勤天数) from 缺勤表 where 代码=a.代码),0) from 资料表 a
select a.*, isnull(金额,0) 金额, isnull(出勤天数,0) 出勤天数, isnull(缺勤天数,0) 缺勤天数 from 资料表 a left join 类别表 b on a.代码=b.代码 left join 出勤表 c on a.代码=c.代码 left join 缺勤表 d on a.代码=d.代码
from 资料表 a,类别表 b,出勤表 c,缺勤表 d
where a.代码 = c.代码 and
a.代码 = d.代码 and
a.类别 = b.类别
JOIN 出勤表 ON 出勤表.代码 = 资料表.代码 INNER JOIN 缺勤表.代码 = 资料表.代码
JOIN 出勤表 ON 出勤表.代码 = 资料表.代码 FULL JOIN 缺勤表.代码 = 资料表.代码谢谢你,,给分!
,金额=(select sum(金额) from 类别表 where 类别=a.类别)
,出勤天数=isnull((select sum(出勤天数) from 出勤表 where 代码=a.代码),0)
,缺勤天数=isnull((select sum(缺勤天数) from 缺勤表 where 代码=a.代码),0)
from 资料表 a
,金额=(select sum(金额) from 类别表 where 类别=a.类别)
,出勤天数=isnull((select sum(出勤天数) from 出勤表 where 代码=a.代码),0)
,缺勤天数=isnull((select sum(缺勤天数) from 缺勤表 where 代码=a.代码),0)
from 资料表 a
select a.*,
isnull(金额,0) 金额,
isnull(出勤天数,0) 出勤天数,
isnull(缺勤天数,0) 缺勤天数
from 资料表 a left join 类别表 b on a.代码=b.代码
left join 出勤表 c on a.代码=c.代码
left join 缺勤表 d on a.代码=d.代码