0
"blog_posts"및 "blog_comments"데이터베이스 테이블이 두 개 있습니다.MySQL 조인 및 COUNT() 모든 레코드를 반환하지 않습니다
blog_posts : 나는이에 대한 댓글이없는 경우가 commentCount 0을 반환 할
SELECT
blog_posts.*,
COUNT(blog_comments.commentID) AS commentCount
FROM
blog_posts
LEFT JOIN blog_comments ON blog_posts.postID = blog_comments.post_id;
:
postID | postTitle | postContent | ...
1 | Hello World | This is a blog post | ...
2 | Lorem Ipsum | This is another blog post | ...
3 | Test Post | This is a third post | ...
blog_comments :
commentID | postID | comment | ...
1 | 1 | Very cool | ...
2 | 1 | Nice | ..
나의 현재 SQL 쿼리는 post가 아니라 쿼리는 mo에 주석이있는 유일한 게시물 인 첫 번째 게시물 만 반환합니다. ment.
어떻게 해결할 수 있습니까?
감사합니다.
감사합니다. 작동 중! – KingOfDog
좋습니다. 안녕하세요, StackOverflow에 오신 것을 환영합니다. 문제에 대한 해결책을 찾은 경우 "수락"및/또는 upvote로 표시하십시오. http://stackoverflow.com/help/someone-answers –
좋습니다. 고맙습니다. :) – KingOfDog