【发布时间】:2013-03-03 17:09:18
【问题描述】:
我有四张桌子 1. tbl_threads 2.tbl_cmets 3. tbl_votes 4. tbl_users
假设当前登录的user_id =3 thread_id = 10
现在我必须检索以下数据
All the fields from tbl_comments where tbl_comments.thread_id =10
All the fields from tbl_users based on the common key tbl_users.user_id = tbl_comments.user_id
All the fields from tbl_votes Where user_id =3 And tbl_votes.comment_id =tbl_comments.comment_id
如何通过一个查询执行所有这些功能?
我尝试了以下查询,但它给了我错误的结果
SELECT tbl_comments.*
, tbl_users.*
, tbl_votes.*
FROM tbl_comments
INNER JOIN tbl_users
on tbl_comments.user_id = tbl_users.user_id
WHERE thread_id= 10
INNER JOIN tbl_votes
on tbl_votes.comment_id = tbl_comments.comment_id
WHERE tbl_votes.user_id= 3
【问题讨论】:
-
请修改您的问题并向我们展示您尝试了什么。你还没有提到 tbl_users
-
该查询是否运行?它如何返回错误的结果?您当前的查询没有选择 where user_id = 3 并且您有冲突的 thread_id 值。
-
@Byron,对不起,我重新更新了我的查询。您可以立即检查。查询运行但它生成重复值而不是所需的结果。实际上,我不知道如何使用带有三个内连接的两个 Where 子句。
-
应该只有一个 where 子句你可以在结尾处执行类似
WHERE thread_id= 10 AND tbl_votes.user_id= 3的操作。 -
您确实应该进行单独的查询。它们都是具有不同基数的不同结果。这些事情应该保持简单。