我有大约20张表,包括学生的考试记录。我试图找出我的问题 (学生ID = sID,即分数1:s1): 学生表:(学生reg.data)
sID name surname regDate photo ........
891 Mike Jackson 01.01.2013 82342984.png ....
表A :(学生成绩)
sID exam s1 s2 s3 s4
891 6 0 0 0 20 > student 891 attended exam 6
891 10 30 80 100 75 > student 891 attended exam 10
(count = 2)
表B :(学生文件,即:文件1:d1)
sID d1 d2 d3 d4 d5 d6
891 true false true true true true
(count = 1)
表C :(学生留言)
mID from to subject message
1 10 891 any sub. any message... > student 891 received messages (look "to")
1 10 891 mes2 other message...
1 29 891 mes3 another message...
(count = 3)
表D :(学生付款)
sID pID amount date details
(此表中没有学生记录,count = 0)
.....和上面的类似表格。
我想要一个如下结果:
sID name surname scoreCount docCount messageCount paymentCount .....
891 Mike Jackson 2 1 3 0 ...
892 Susan Button 0 3 10 0 ...
893 Ahmad Malisi 1 0 5 2 ...
894 any any 4 1 0 0 ...
...
..
答案 0 :(得分:2)
你试过这个吗?
Select sid,name,surname,
(Select count(*) From student_scores Where Sid = S.Sid ) as scoreCount ,
(Select count(*) From student_documents Where Sid = S.Sid ) as docCount ,
(Select count(*) From student_messages Where to= S.Sid ) as messageCount ,
(Select count(*) From student_payments Where Sid = S.Sid ) as paymentCount ,
.
.
.
From StudentsTables S
Where Sid=891
答案 1 :(得分:0)
Select sid,name,surname,
isnull(sscnt,0) ,isnull(sdcnt ,0) ,isnull(spcnt ,0) ,isnull(smcnt,0)
From StudentsTables S
join (Select Sid,count(Sid) sscnt From student_scores group by Sid) ss
on ss.sid=s.sid
join (Select Sid,count(Sid) sdcnt From student_documents group by Sid) sd
on sd.sid=s.sid
join (Select Sid,count(Sid) spcnt From student_payments group by Sid) sp
on sp.sid=s.sid
join (Select Sid,count(Sid) smcnt From student_messages group by Sid) sm
on sm.sid=s.sid
这将查询所有SId(s)