2012-01-05 2 views
0

입니다 I 사용자가 게스트로뿐만 아니라 코멘트를 게시 할 수있는이 댓글 테이블이 결과에 가입 인출하는 방법을, 내가 가져올 수있는 사용자의 의견은 게스트 사용자가없는 것입니다 사용자 ID 따라서 해당 ID가 0으로 db로 이동하므로 표시되지 않습니다. 사용자와 게스트 모두의 의견을 어떻게 표시합니까? 더는 comments.user_id에 대한 user 일치, 다음 users.username는 NULL이됩니다이 없다면MySQL은 id가 0

답변

2

당신은 LEFT JOIN

SELECT `comments`.`user_id`, `comments`.`pubdate`, `comments`.`comment`, 
     `comments`.`guest_name`, 
     `comments`.`id`, 
     `users`.`username`, 

FROM `comments` 
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`user_id` 
WHERE `item_id` = '64' AND `section` = 'blog' 

를 사용해야합니다.

1

사용 왼쪽 가입 :

SELECT `comments`.`user_id`, `comments`.`pubdate`, `comments`.`comment`, 
`comments`.`guest_name`, 
`comments`.`id`, 
`users`.`username`, 

FROM (`comments`) 
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`user_id` 
WHERE `item_id` = '64' AND `section` = 'blog' 
0

는 LEFT를 수행하지 않는 간단한, 가입.

0
 

SELECT `comments`.`user_id`, `comments`.`pubdate`, `comments`.`comment`, 
     `comments`.`guest_name`, `comments`.`id`, `users`.`username` 
FROM `comments` 
LEFT JOIN `users` ON (`comments`.`user_id` = `users`.`user_id`) 
WHERE `item_id` = '64' AND `section` = 'blog'