2013-07-01 2 views
-4

나는 오류가 날 :) 도와주세요 위치를 모르는 알 수없는 열 'countf'# 1054 - 'where 절'

SELECT a.*,b.*,users.*, 
    (SELECT msg_text FROM message_private p 
    WHERE p.group_id=a.group_id ORDER BY occured_at DESC LIMIT 1) as message, 
    (SELECT COUNT(f.profile_id) as countf from message_view f WHERE f.profile_id = 'sN07X2' AND f.id_group = b.group_id) 
FROM message_group a 
JOIN message_group b ON a.group_id=b.group_id 
INNER JOIN users ON users.profile_id = b.profile_id 
WHERE a.profile_id = 'sN07X2' 
AND b.profile_id != a.profile_id AND countf != 0 ORDER BY a.message_group_id DESC LIMIT 9 
+0

오류 메시지를 제공하시기 바랍니다 ... –

+0

@DanielRobertus 오류 메시지가 제목입니다 .. – Daanvn

+0

당신은에서 별칭을 사용할 수 없습니다 WHERE 절을 선택하십시오. – Taryn

답변

0

문제는 당신이에서 열 별칭을 참조하려고하는 것입니다 WHERE 절의 SELECT 목록 당신은 결과를 얻기 위하여는 message_view 테이블과 조인을 사용보고 할 수 있습니다, 당신은 별명에 필터링 할 수 있습니다

SELECT a.*,b.*,users.*, 
    (SELECT msg_text 
    FROM message_private p 
    WHERE p.group_id=a.group_id 
    ORDER BY occured_at DESC LIMIT 1) as message, 
    f.countf 
FROM message_group a 
INNER JOIN message_group b ON a.group_id=b.group_id 
INNER JOIN users ON users.profile_id = b.profile_id 
LEFT JOIN 
(
    SELECT COUNT(profile_id) countf, id_group 
    FROM message_view 
    WHERE profile_id = 'sN07X2' 
    GROUP BY id_group 
) f 
    on f.id_group = b.group_id 
WHERE a.profile_id = 'sN07X2' 
    AND b.profile_id != a.profile_id 
    AND countf != 0 
ORDER BY a.message_group_id DESC 
LIMIT 9 
+0

감사합니다. 그러나이 오류는 다음과 같습니다 : # 1064 - SQL 구문에 오류가 있습니다. 'WHERE a.profile_id ='sN07X2 '및 b.profile_id! = a.profile_id AND 17'at co '근처에서 사용할 올바른 구문에 대한 MySQL 서버 버전에 해당하는 설명서를 확인하십시오. –

+0

@FabrizioFenoglio 가입, 내 편집보기 – Taryn

0

문제는 하위 쿼리 countf를 정의하고가 참조하려고한다는 것입니다 그것은 where 절에 있습니다. 대신, 외부 선택의 as countf이 있고 having 절을 사용할 필요가 :

SELECT a.*,b.*,users.*, 
    (SELECT msg_text FROM message_private p 
    WHERE p.group_id=a.group_id ORDER BY occured_at DESC LIMIT 1) as message, 
    (SELECT COUNT(f.profile_id) as countf from message_view f WHERE f.profile_id = 'sN07X2' AND f.id_group = b.group_id 
    ) as countf 
FROM message_group a 
JOIN message_group b ON a.group_id=b.group_id 
INNER JOIN users ON users.profile_id = b.profile_id 
WHERE a.profile_id = 'sN07X2' 
AND b.profile_id != a.profile_id 
having countf <> 0 
ORDER BY a.message_group_id DESC LIMIT 9