1

我有以下查询

    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

2 回答 2

4

您的派生列 count_absent 在 SELECT 子句中处理,该子句在 WHERE 子句之后处理,因此不可用。如果您将整个查询包装在一个外部 SELECT 中,那么它将可用,以便能够访问派生列。

IE

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
于 2009-10-26T16:23:02.987 回答
2

我不确定您要在这里完成什么,但请注意,给出 count_absent 的嵌入式查询除了“absent = 1”之外没有使用任何标准。所以不管你看的是哪个学生,你总是会得到所有 student_attendance 记录的数量,缺席=1。我怀疑您想将此限制为所选学生的 student_attendance 记录。

此外,您可能会调查“加入”子句。这将使您的查询更容易理解,然后旧样式与 where 连接。

于 2009-10-26T16:45:34.203 回答