-1

我需要从博客作者为一定数量的数据库中获取所有评论。除了我的 SQL 查询没有使用正确的作者 ID。

这是我的查询:

SELECT em_comments.*, em_users.nickname
FROM em_comments
INNER JOIN em_posts ON em_posts.id = em_comments.post
INNER JOIN em_users ON em_users.id = em_comments.author
WHERE em_posts.author = 1
ORDER BY date DESC 

如您所见,它应该只获取帖子作者为1的帖子的评论。我返回的结果是4条评论,但总共有6条评论应该从数据库中恢复。

我知道问题出在某个地方

WHERE em_posts.author = 1

因为这个查询如何只返回评论作者为 1 的评论。但它应该返回帖子作者为 1 的所有帖子的所有评论。

有人对我如何解决这个问题有任何想法吗?

提前致谢,马克

编辑:

评论表结构+数据

id | post | author | date                 | content
6    7      1       2012-05-10 12:30:25     Dat weet hij zelf ook xD
5    6      1       2012-05-10 12:30:12     yup
1    1      1       2012-05-09 13:39:34     Whoop Whoop! Eerste comment test! xD
2    1      1       2012-05-10 12:27:49     Nice. tweede comment werkt nu ook
3    1      1       2012-05-10 12:29:45     Nice. tweede comment werkt nu ook 
4    1      1       2012-05-10 12:30:00     Nice. tweede comment werkt nu ook

帖子表结构+数据

id | author | date                | content                                                 | title    
7    1        2012-05-09 19:42:33   Mark is nog topper geworden vandaag. xD                   Mark is nog cooler
6    1        2012-05-09 19:30:27   Mark is top!                                              Mark
1    1        2012-05-07 19:21:03   lol test met 'quotes' en "dubbele q...     Whoop Whoop!

users表结构+数据

id | nickname
1    Mark
2    Kevin
4

1 回答 1

1

内连接排除在 ON 子句中具有 NULL 值的行。检查列中行的值以查看是否有任何出现 NULL。

于 2012-05-10T13:03:55.907 回答