Database/MySQL
MySQL ) 중복된 결과를 제거하는 distinct
Yusang
2021. 3. 9. 15:13
MySQL 중복된 결과를 제거하는 distinct
아래와 같이 Community라는 테이블에 내가 작성한 글들의 책이름과 책 인덱스를 출력하도록 쿼리를 짰습니다.
하지만 문제점이 있습니다.
14번 인덱스에 해당하는 '부의 추월차선'에 관련된 글을 두 개 작성했기 때문에 중복된 출력 결과가 나오게 됩니다.
select Community.bookIdx,bookName from Book
left outer join Community on Community.bookIdx = Book.bookIdx
where userIdx = ? and Community.status = 1 and Book.status =1;

distinct를 사용하면 이러한 중복된 결과를 제거하고 출력할 수 있습니다.

select distinct Community.bookIdx,bookName from Book
left outer join Community on Community.bookIdx = Book.bookIdx
where userIdx = ? and Community.status = 1 and Book.status =1;
내가 쓴 글을 조회하는 API를 설계할 때 참고할 수 있을 거 같습니다 ~
