2009-10-26 70 views
1

我有以下查詢MySQL的未知列

SELECT 
    s.name, 
    s.surname, 
    s.id_nr, 
    s.student_nr, 
    s.createdate, 
    s.enddate, 
    (SELECT count(*) FROM Student_Attendance WHERE absent = 1) AS count_absent 
    FROM 
    Student AS s, 
    Student_Contact AS sc, 
    Student_Payment AS p, 
    Student_Courses AS scou, 
    Modules AS m, 
    Student_Certificate AS scer, 
    Student_Results AS sr, 
    Lecturer_Profile AS l, 
    Lecturer_Comments AS lc, 
    Student_Attendance AS sa, 
    Student_Training AS t 
    WHERE s.s_id = sc.s_id 
    AND s.s_id = p.s_id 
    AND s.s_id = scou.s_id 
    AND scou.c_id = m.c_id 
    AND s.s_id = scer.s_id 
    AND s.s_id = sr.s_id 
    AND s.s_id = lc.s_id 
    AND lc.l_id = l.l_id 
    AND s.s_id = sa.s_id 
    AND LOWER(s.name) = 'andile' 
    AND LOWER(s.surname) = ' orson vulture' 
    AND s.id_nr = 8403125062671 
    AND LOWER(sc.race) = 'white' 
    AND sc.gender = 1 
    AND LOWER(sc.area) = 'gauteng' 
    AND p.payment_type = 1 
    AND s.student_nr = 203087506 
    AND scou.c_id = 1 AND sc.age = 23 
    AND scer.certificate_number = 3424234 
    AND sr.result = 32 
    AND l.l_id= 1 
    AND count_absent = 3 
    AND LOWER(s.branch) = 'pretoria' 
    AND LOWER(s.campus_name) = 'pretoria' 
    AND LOWER(sc.kin_name) = 'self' 
    AND t.s_id = s.s_id 
    AND t.sp_id = 1 

,我得到以下錯誤

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count_absent' in 'where clause' 

不知道如何解決這個問題,因爲該列在select語句

回答

4

您的派生列count_absent在SELECT子句中處理,該子句在WHERE子句之後處理,因此不可用。如果您將整個查詢包裝在外部SELECT中,以便能夠訪問派生列,它將可用。

select * from (
SELECT 
s.name, 
s.surname, 
s.id_nr, 
s.student_nr, 
s.createdate, 
s.enddate, 
(SELECT count(*) FROM Student_Attendance WHERE absent = 1) AS count_absent 
FROM 
Student AS s, 
Student_Contact AS sc, 
Student_Payment AS p, 
Student_Courses AS scou, 
Modules AS m, 
Student_Certificate AS scer, 
Student_Results AS sr, 
Lecturer_Profile AS l, 
Lecturer_Comments AS lc, 
Student_Attendance AS sa, 
Student_Training AS t 
WHERE s.s_id = sc.s_id 
AND s.s_id = p.s_id 
... 
AND t.s_id = s.s_id 
AND t.sp_id = 1 
) as x where count_absent = 3 
2

我不知道你想什麼,在這裏完成,但要注意,讓count_absent嵌入式查詢使用不超過「缺席= 1」等標準。所以,你在看哪個學生並不重要,你總能得到缺席= 1的所有student_attendance記錄。我懷疑你想限制這個選擇學生的student_attendance記錄。

此外,您可能會調查「join」子句。這將使您的查詢更容易理解舊式與何處的聯接。