簡體   English   中英

mysql用where子句計算來自另一個表的行

[英]mysql count rows from another table with a where clause

嗨,大家需要一些有關的幫助

我如何計算帶有where子句的另一個表中的行

所以我有兩張桌子

表學生和出勤表我要計算與該學生對應的所有出勤表

這是桌子學生

在此處輸入圖片說明

這是表出席

在此處輸入圖片說明

我嘗試了這個,但是拿來有出勤的學生

SELECT student.StudentID, student.`Name`, COUNT(attendance.AttendanceID) AS Total
FROM student
LEFT JOIN attendance ON student.StudentID = attendance.StudentID
where DateEntered between '2018-10-01' and '2018-10-01' 
GROUP BY student.StudentID,student.`Name`

我想得到的是獲取所有學生並計數他/她有多少次出勤的次數。 在輸入日期的地方輸入A

在此處輸入圖片說明

謝謝你的幫助.......

將條件放在左連接子句中:

SELECT student.StudentID, student.`Name`, COUNT(attendance.AttendanceID) AS Total
FROM student
LEFT JOIN attendance ON student.StudentID = attendance.StudentID
    and DateEntered between '2018-10-01' and '2018-10-01' 
GROUP BY student.StudentID,student.`Name`

您可以對參加student的出勤率進行匯總查詢:

SELECT     s.StudentID, 
           s.Name,
           COALESCE(Total, 0) 
FROM       student s
LEFT JOIN  (SELECT StudentId, COUNT(*) AS total
            FROM   attendance
            GROUP BY StudentId) ON s.StudentId = a.StudentId
WHERE      DateEntered BETWEEN '2018-10-01' and '2018-10-01'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM