我想要实现的是选择只有我 friend 写的评论。我有以下表格。
friend
id
my_user_id
friend_user_id
评论
id
user_id
comment
到目前为止我就是这样做的。
首先在 friend 表中找出谁是我的 friend :
mysql_query("SELECT friend_user_id FROM friends
WHERE my_user_id = $user_id ");
然后我选择了只有我 friend 写的评论。
mysql_query("SELECT * FROM comments WHERE user_id = 'my_first_friends_id'
OR user_id = 'my_second_friends_id' ");
现在,这是蹩脚的版本,而且速度很慢。我在评论表中有数百万条条目。
这个问题的最佳解决方案是什么?如何在一个查询中解决这个问题?速度也很重要。
最佳答案
select c.id, c.user_id, c.comment
from Friends f
inner join Comments c
on f.friend_user_id = c.user_id
where f.my_user_id = $user_id
关于mysql - 如何查询多个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4962784/