SELECT DISTINCT aa.subject_code ,aa.user_code ,aa.another_user_code ,aa.message_uid ,aa.user_delete ,aa.message_accept ,aa.message_reject ,bb.user_nick ,bb.mylocation ,cc.subject ,cc.study_prepar ,cc.school_year ,dd.univers_name ,dd.first_age ,dd.last_age ,dd.user_age ,dd.user_gender ,dd.demo_lesson ,dd.face_lesson ,dd.full_lesson ,dd.lesson_price ,dd.first_price ,dd.last_price ,ff.price ,SUBSTRING(message_time,3,14) AS message_time ,(SELECT COUNT(message_confirm)FROM message WHERE aa.subject_code =ee.subject_code AND aa.another_user_code = ee.another_user_code AND message_confirm = 'N')AS message_confirm FROM message_user AS aa LEFT OUTER JOIN user AS bb ON aa.another_user_code = bb.user_code JOIN subjects AS cc ON aa.subject_code = cc.subject_code JOIN stucondition AS dd ON aa.subject_code = dd.subject_code LEFT OUTER JOIN matching AS ff ON aa.subject_code = ff.subject_code LEFT OUTER join message AS ee on aa.subject_code = ee.subject_code WHERE aa.user_code = '5' AND aa.user_delete = 0
메세지 방 리스트를 불러오는 쿼리입니다...
메세지 인원은 (user_code 본인) , (another_user_code 상대방 입니다)
메세지 방 리스트를 불러올때 substring 을 써서 마지막 메세지의 시간을 가저오고 있구요!
서브쿼리를 써서 카운트를 하려고 하는데 오류가 있는 것 같아서 질문드립니다...
현재 이렇게 대화방이 있습니다
5 7
5 6
message_confirm 이 읽음처리이고 N으로 되어있는 거만 카운트 되게 하려고 했는데
저렇게 쿼리를 써버리니 5,7번 5.6번 방의 합산된 카운트를 가저오게 되어서 서브쿼리의
aa.subject_code =ee.subject_code 이부분이 잘못된건지 도통 감이 잡히지 않아서 질문드립니다 ㅠㅠ
현재 5번과 연결된 채팅방이 2개 이기 때문에
6번 1개 7번 1개가 나와야 하는데 쿼리도 어디서 꼬인 것 같은데 어디서 꼬인지 모르겠어서 ㅠㅠ
도움 주시면 감사하겠습니다
테이블은 현재 아래 두개를 쓰고 있습니다..
Distinct 는 왜 쓰나요?
- 필요에 의해서 정확한 의도를 가지고 쓴 것인지?
- 쿼리를 잘못 작성하여 발생된 중복을 억지로 커버하려고 쓴 것인지?
대부분의 경우 Distinct 가 필요한 경우는 거의 없습니다.
잘못된 조인이 원인일 듯 합니다.
메인쿼리에 message AS ee 에 대한 조인을 제거하고,
서브쿼리에 message 에 대해 별칭 ee 를 부여해야 합니다.
distinct 는 중복처리 된 값때문에 쓰게 되었습니다 ㅠㅠ 도움주셔서 감사합니다 풀어보겠습니다!