SELECT子句中的子查询并解决“子查询返回多行”

时间:2015-01-10 09:54:32

标签: mysql select subquery

期望的输出:

2014-11-12 2 
2014-11-18 2 
2014-11-23 1

我的代码:

SELECT Attendance.Attend_Date, (SELECT COUNT(Attendance.AttendDet_Type) as P 
FROM Student, Attendance 
WHERE Student.Stud_ID = Attendance.Stud_ID 
AND Student.Stud_Class = '1A1' 
AND Attendance.Attend_Date BETWEEN '2014-01-01' AND '2014-12-01' 
AND Attendance.AttendDet_Type = 'P' 
GROUP BY Attendance.Attend_Date
) as T
FROM Student, Attendance
WHERE Student.Stud_ID = Attendance.Stud_ID
AND Student.Stud_Class = '1A1'
AND Attendance.Attend_Date BETWEEN '2014-01-01' AND '2014-12-01'
GROUP BY Attendance.Attend_Date

任何人都可以帮助我吗?感谢。

1 个答案:

答案 0 :(得分:0)

我的回答。谢谢你们!

SELECT Attendance.Attend_Date, 
COUNT(CASE WHEN Attendance.AttendDet_Type = 'P' THEN Attendance.Attend_Date ELSE null END)AS P, 
COUNT(CASE WHEN Attendance.AttendDet_Type = 'A' THEN Attendance.Attend_Date ELSE null END)AS A, 
COUNT(CASE WHEN Attendance.AttendDet_Type = 'C' THEN Attendance.Attend_Date ELSE null END)AS C, 
COUNT(CASE WHEN Attendance.AttendDet_Type = 'MC' THEN Attendance.Attend_Date ELSE null END)AS MC 
FROM Student, Attendance 
WHERE Student.Stud_ID = Attendance.Stud_ID 
AND Student.Stud_Class = '" & DropDownList13.SelectedValue & "' 
AND Attendance.Attend_Date BETWEEN '" & Label5.Text & "' 
AND '" & Label6.Text & "' 
GROUP BY Attendance.Attend_Date