문제

다음 쿼리가 있습니다

    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 문에 있으므로 이것을 해결하는 방법을 모릅니다.

도움이 되었습니까?

해결책

파생 된 열 count_absent는 select 절에서 처리되며, 여기서 where 절후에 처리되므로 사용할 수 없습니다. 파생 된 열에 액세스 할 수 있도록 전체 쿼리를 외부 선택에 래한 경우 사용할 수 있습니다.

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

다른 팁

여기서 달성하려는 내용은 확실하지 않지만 count_absent를 제공하는 임베디드 쿼리는 "결석 = 1"이외의 기준을 사용하지 않습니다. 따라서 어떤 학생을보고 있는지는 중요하지 않습니다. 항상 모든 Student_attendance Records = 1이없는 모든 Student_attendance Records를 얻게됩니다. 선택한 학생의 Student_attendance Records로 제한하고 싶다고 생각합니다.

또한 "조인"절을 조사 할 수도 있습니다. 이렇게하면 쿼리를 훨씬 쉽게 이해하기가 훨씬 쉬워집니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top