我有以下查询
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语句中
,因此不知道如何解决这个问题答案 0 :(得分: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
答案 1 :(得分:2)
我不确定你要在这里完成什么,但请注意,给count_absent的嵌入式查询除了“absent = 1”之外不使用任何标准。因此,无论您正在查看哪个学生,您都将获得缺少= 1的所有student_attendance记录的计数。我怀疑你想把它限制在所选学生的student_attendance记录中。
另外,您可以调查“join”子句。这将使您的查询更容易理解当时加入where旧样式。