【发布时间】:2019-12-22 00:48:25
【问题描述】:
谁能帮帮我?
我有 4 个表 posts、users、users details 和 cmets。
结构如下:
帖子
- 身份证
- 标题
- 说明
用户
- 身份证
- 用户名
- 密码
users_details
- 身份证
- user_id
- 头像
- 位置
cmets
- 身份证
- post_id
- user_id
- 父母
- 评论
- 已发布
我有一个查询,我想在特定帖子上显示 cmets。
这是我写的,但头像栏没有显示任何结果。看来我在加入什么的时候写错了。
SELECT comments.id as comment_id,
comments.user_id as user_id,
comments.post_id as post_id,
comments.parent as parent,
comments.comment as comment,
comments.posted as posted,
users_details.avatar as avatar,
users.username as username
FROM `comments`
LEFT JOIN users ON users.id = comments.user_id
LEFT JOIN users_details ON users_details.user_id = users.id
WHERE comments.post_id='60337'
ORDER BY posted ASC
这是带有结果https://i.imgur.com/VQ8KwV1.png的表格截图
我想提一下头像字段,它在 users_details 表中为该用户提供了一个值,因此应该显示它...
谢谢!
【问题讨论】:
-
在您提供的设计中,我没有看到 cmets 表中的 user_id 列?
-
好的,对不起,我忘了提,但它就在那里。我现在要编辑。
-
此查询是否返回任何数据或返回 NULL --> 从 User_Details 中选择 Avatar,其中 User_Id in (2,9212)
-
你说得对,我在更新 users_details 表时遇到问题,并且 users_details 中的 user_id 与用户的 id 不匹配...
标签: mysql