2012-01-05 13 views
0

である私は、ユーザーがゲストとしてだけでなくコメントを投稿することができ、このコメントテーブルを持っている結果を参加フェッチする方法を、私は取得することができ、ユーザーのコメントは、ゲストユーザが持っていないですユーザーID、したがってidは0にdbに移動するので、表示されません。どのようにユーザーとゲストの両方のコメントを表示できますか?mysqlはIDが0

答えて

2

あなたは一致usercomments.user_idのためにそこにいない場合は、users.usernameはNULLになります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 JOINのではなく、シンプルないずれかの操作を行います。

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'