我想获取用户在不同表中输入的记录数。 DB的架构是:
+-----------------------+
| Survey Master |
+----------------+------+
| Field | Key |
+----------------+------+
| id | PK |
| Username | |
| FamilyMasterId | FK |
+----------------+------+
+------------+------+
| Family Master |
+------------+------+
| Field | Key |
+------------+------+
| id | PK |
+------------+------+
+-----------------------+
| Family Detail |
+----------------+------+
| Field | Key |
+----------------+------+
| id | PK |
| FamilyMasterId | FK |
+----------------+------+
+-----------------------+
| Travel Master |
+----------------+------+
| Field | Key |
+----------------+------+
| id | PK |
| FamilyDetailId | FK |
+----------------+------+
+-----------------------+
| Travel Detail |
+----------------+------+
| Field | Key |
+----------------+------+
| id | PK |
| TravelMasterId | FK |
+----------------+------+
我希望看到每个用户在每个表中创建的记录数量如下:
Username SurveyMaster FamilyMaster FamilyDetail TravelMaster TravelDetail
---------- -------------- -------------- -------------- -------------- --------------
User001 59 47 36 26 12
User002 88 76 64 42 25
User003 49 44 35 25 15
User004 77 69 55 45 37
查看以下链接后:
我能够编写此查询但它在所有列中提供相同的记录:
SELECT USERNAME, COUNT(USERNAME) SURVEYMASTER, COUNT(USERNAME) FAMILYMASTER, COUNT(USERNAME) FAMILYDETAIL, COUNT(USERNAME) TRAVELMASTER, COUNT(USERNAME) TRAVELDETAIL FROM
((SELECT CREATEUSER USERNAME FROM SURVEYMASTER
)
UNION ALL
(SELECT SM.CREATEUSER USERNAME FROM SURVEYMASTER SM
INNER JOIN FAMILYMASTER FM ON FM.ID = SM.FAMILYMASTERID
)
UNION ALL
(SELECT SM.CREATEUSER USERNAME FROM SURVEYMASTER SM
INNER JOIN FAMILYMASTER FM ON FM.ID = SM.FAMILYMASTERID
INNER JOIN FAMILYDETAIL FD ON FM.ID = FD.FAMILYMASTERID
)
UNION ALL
(SELECT SM.CREATEUSER USERNAME FROM SURVEYMASTER SM
INNER JOIN FAMILYMASTER FM ON FM.ID = SM.FAMILYMASTERID
INNER JOIN FAMILYDETAIL FD ON FM.ID = FD.FAMILYMASTERID
INNER JOIN TRAVELMASTER TM ON FD.ID = TM.FAMILYDETAILID
)
UNION ALL
(SELECT SM.CREATEUSER USERNAME FROM SURVEYMASTER SM
INNER JOIN FAMILYMASTER FM ON FM.ID = SM.FAMILYMASTERID
INNER JOIN FAMILYDETAIL FD ON FM.ID = FD.FAMILYMASTERID
INNER JOIN TRAVELMASTER TM ON FD.ID = TM.FAMILYDETAILID
INNER JOIN TRAVELDETAIL TD ON TM.ID = TD.TRAVELMASTERID
)
) T
GROUP BY USERNAME
ORDER BY USERNAME
修改
以下是关系描述:
答案 0 :(得分:1)
如果我们考虑性能但是它可以提供所需的结果,这可能不是完美的解决方案
SELECT sm.Username ,
COUNT(*) SurveyMaster ,
COUNT(FamilyMasterId) FamilyMaster ,
fd.FamilyDetail ,
tm.TravelMaster ,
td.TravelDetail
FROM SurveyMaster sm
JOIN ( SELECT Username ,
COUNT(fd.id) FamilyDetail
FROM SurveyMaster sm
JOIN FamilyMaster fm ON sm.FamilyMasterId = fm.Id
JOIN FamilyDetail fd ON fm.id = fd.FamilyMasterId
GROUP BY Username
) fd ON sm.Username = fd.Username
JOIN ( SELECT Username ,
COUNT(tm.id) TravelMaster
FROM SurveyMaster sm
JOIN FamilyMaster fm ON sm.FamilyMasterId = fm.Id
JOIN FamilyDetail fd ON fm.id = fd.FamilyMasterId
JOIN TravelMaster tm ON fd.Id = tm.FamilyDetailId
GROUP BY Username
) tm ON sm.Username = tm.Username
JOIN ( SELECT Username ,
COUNT(td.id) TravelDetail
FROM SurveyMaster sm
JOIN FamilyMaster fm ON sm.FamilyMasterId = fm.Id
JOIN FamilyDetail fd ON fm.id = fd.FamilyMasterId
JOIN TravelMaster tm ON fd.Id = tm.FamilyDetailId
JOIN TravelDetail td ON tm.Id = td.TravelMasterId
GROUP BY Username
) td ON sm.Username = td.Username
GROUP BY sm.Username ,
fd.FamilyDetail ,
tm.TravelMaster ,
td.TravelDetail;