2

目前我有这个mysql:

$result = mysql_query("SELECT blog.id, blog_comments.cid, blog.name,
  blog.blogtext, blog.date, blog.like, blog.dislike, blog_comments.comment, blog_comments.date
FROM blog, blog_comments  
WHERE blog.id = blog_comments.cid   
ORDER BY blog.id DESC LIMIT 30")

该查询返回行及其注释,问题是:它只返回带有注释的行,它不返回没有注释的行。我找到了一些主题,并发现我可能应该使用FULL OUTER JOIN. 现在我试图应用这个,没有成功。

非常感谢您的帮助

4

1 回答 1

3

而不是一个隐式连接(逗号分隔的表列表产生一个INNER JOIN),使用一个显式的LEFT JOIN

SELECT 
  blog.id,
  blog_comments.cid,
  blog.name,
  blog.blogtext,
  blog.date, 
  blog.like, 
  blog.dislike, 
  blog_comments.comment, 
  blog_comments.date
FROM 
 blog
 /* LEFT JOIN will return rows from blog (the "left" side of the join) even if no 
    related rows exist in blog_comments (the "right" side of the join) */
 LEFT JOIN blog_comments ON blog.id = blog_comments.cid   
ORDER BY blog.id DESC
LIMIT 30
于 2012-08-12T22:25:11.883 回答