太晚了,我的头很痛,所以我希望能得到一点帮助。
我有3个表名为:任务、用户、注释
在注释表中,我有两个名为user_id和task_id的列。
这里的主要特性是,我可以单击一个任务,然后获取该任务的所有注释。到目前为止,它正在使用这个SQL:
SELECT * FROM comments JOIN task where task.id=task_id我可以很容易地打印出我的结果,并查看所选任务的所有注释。但是,我只能看到属于任务的用户的id。
我错过了与用户表的连接,并显示了用户名,其中user_id =指向用户的id,而不仅仅是id,因此我想显示用户名而不是用户id。
我试过这样的方法,但没有用:
SELECT comments.*, users.username,
FROM comments, users
WHERE users.id = user_id
JOIN task where task.id=task_id发布于 2014-11-30 01:48:49
尝试以下示例查询
SELECT comments.*, users.username,
FROM comments C INNER JOIN task t ON C.task_id=t.task_id
INNER JOIN users U on C.user_id = u.user_id
WHERE t.task_id = @task_id //(this is variable, pass your task value here)https://stackoverflow.com/questions/27209108
复制相似问题