나의 게시글을 보여줄 jsp를 만들었고, boardId를 4번으로 지정하여 boardId 4번으로 호출 될 때 나의 게시글을 보여주려고 한 것이 원래 목표였다.
그런데 boardId를 파라미터로 넘겨줘서 쿼리까지 잘 보냈지만 나의게시글의 게시판에 게시글은 0개로 보여지는 문제가 발생...
회원1로 로그인을 했고 회원1이 쓴 게시글은 모두 보여진다!
같은 방식으로 나의질문, 나의댓글 모아보기 리스트를 구현했다.
나의 댓글 불러오기 목록으로 들어가면, 내가 특정 게시글에 댓글을 2개를 적던 3개를 적던 100개를 적던, 게시글 제목 옆에는 댓글갯수가 1로만 표시되는 문제가 발생했다!
댓글 갯수를 불러오는 쿼리에서 잘못된 것 같았고..
내가 쓴 댓글의 원글이 중복으로 보여지는 문제를 해결하고자 DISTINCT를 사용한 것이 문제인걸까?
<script>
SELECT DISTINCT A.*, M.nickname AS extra__writer, IFNULL(COUNT(R.id),0) AS extra__repliesCnt
FROM `reply` AS R
INNER JOIN `member` AS M
ON R.memberId = M.id
LEFT JOIN article AS A
ON A.id = R.relId
WHERE R.memberId = 2
AND 1
<if test="searchKeyword != ''">
<choose>
<when test="searchKeywordTypeCode == 'title'">
AND A.title LIKE CONCAT('%',#{searchKeyword},'%')
</when>
<when test="searchKeywordTypeCode == 'body'">
AND A.body LIKE CONCAT('%',#{searchKeyword},'%')
</when>
<otherwise>
AND A.title LIKE CONCAT('%',#{searchKeyword},'%')
OR A.body LIKE CONCAT('%',#{searchKeyword},'%')
</otherwise>
</choose>
</if>
GROUP BY R.id
ORDER BY R.id DESC
<if test="limitFrom >= 0 ">
LIMIT #{limitFrom}, #{limitTake}
</if>
</script>
쿼리 문제가 맞았고, DISTINCT를 사용할 필요도 없었다.
내가 그룹화하려는 기준을 잘못잡은 것이다. 나는 댓글의 id를 기준으로 그룹화를 했었고,
게시글의 id를 기준으로 그룹화를 했어야 이 문제가 해결되는 것!
<script>
SELECT A.*, M.nickname AS extra__writer, IFNULL(COUNT(R.id),0) AS extra__repliesCnt
FROM `reply` AS R
INNER JOIN `member` AS M
ON R.memberId = M.id
LEFT JOIN article AS A
ON A.id = R.relId
WHERE R.memberId = 2
AND 1
<if test="searchKeyword != ''">
<choose>
<when test="searchKeywordTypeCode == 'title'">
AND A.title LIKE CONCAT('%',#{searchKeyword},'%')
</when>
<when test="searchKeywordTypeCode == 'body'">
AND A.body LIKE CONCAT('%',#{searchKeyword},'%')
</when>
<otherwise>
AND A.title LIKE CONCAT('%',#{searchKeyword},'%')
OR A.body LIKE CONCAT('%',#{searchKeyword},'%')
</otherwise>
</choose>
</if>
GROUP BY A.id
ORDER BY R.id DESC
<if test="limitFrom >= 0 ">
LIMIT #{limitFrom}, #{limitTake}
</if>
</script>
내가 쓴 댓글 수 만큼 보여지고, 원게시글 마다 내가 몇개의 댓글을 적었는지 게시글 제목 옆에 잘 표시가 된다!