我有以下表格:
架构
tbl_semester_empid {id,semester_name varchar(50),start_date}
tbl_batch_empid {id,term_id,batch_name,session_room}
tbl_associate_empid {id, associate_name,batch_id,联系,join_date,induction_result,stream_result int}
问题查询是:显示学期明智的员工人数。
我写了 3 个查询:
--basic query
SELECT s.ID "Semester Id", COUNT(*) "Associate Count" FROM TBL_SEMESTER_593932 s, TBL_ASSOCIATE_593932 a, TBL_BATCH_593932 b
WHERE s.ID=b.SEMESTER_ID AND a.BATCH_ID=b.ID
GROUP BY s.ID;
--used NVL function
SELECT s.ID "Semester Id", NVL(COUNT(*),0) "Associate Count" FROM TBL_SEMESTER_593932 s, TBL_ASSOCIATE_593932 a, TBL_BATCH_593932 b
WHERE s.ID=b.SEMESTER_ID AND a.BATCH_ID=b.ID
GROUP BY s.ID;
-- used LEFT OUTER JOIN and JOIN
SELECT s.ID "Semester Id", NVL(COUNT(*),0) "Associate Count"
FROM TBL_SEMESTER_593932 s LEFT OUTER JOIN TBL_BATCH_593932 b ON s.ID=b.SEMESTER_ID
JOIN TBL_ASSOCIATE_593932 a ON a.BATCH_ID=b.ID
GROUP BY s.ID;
它们都显示以下输出(它只显示对应于其中至少有 1 个学生的那些学期的记录,如果该学期没有学生,则不显示该学期的信息):
我希望在 Associates Count 列中没有员工的学期为 0。所以我用两个 LEFT OUTE JOIN 尝试了同样的方法:
SELECT s.ID "Semester Id", NVL(COUNT(*),0) "Associate Count"
FROM TBL_SEMESTER_593932 s LEFT OUTER JOIN TBL_BATCH_593932 b ON s.ID=b.SEMESTER_ID
LEFT OUTER JOIN TBL_ASSOCIATE_593932 a ON a.BATCH_ID=b.ID
GROUP BY s.ID;
然而结果很奇怪: