我的mysql数据库上有3个表(用户,关注者,帖子),我写了一个sql,从用户跟随的人那里得到所有帖子。
SQL
DaysDifference
结果
<a target="_blank" href="@baseLogsUrl/#/discover?_g=(time:(from:no@DaysDifference/d,to:now))&_a=(query:(query_string:(query:'@data._id')))">
用户表
SELECT post.* FROM post JOIN
follower ON post.owner_user_id = follower.user_id AND
follower.follower_id = "3"
关注者表
id | owner_user_id | content
2 | 1 | why are all my senior developers jerks?
3 | 1 | PHP7 in your face node.js
发布表格
id | username | password
1 | user1 | 12345678
2 | user2 | 12345678
3 | user3 | 12345678
所以现在我正在尝试选择用户关注的帖子和用户的帖子
我试过这个sql
SQL
user_id | follower_id
3 | 1
3 | 2
1 | 3
结果
id | owner_user_id | content
1 | 2 | reading a 1k+ page on mysql, do i need to?
2 | 1 | why are all my senior developers jerks?
3 | 1 | PHP7!, in your face node.js
3 | 3 | I posted
请尝试用sql实现, if(possible){&#34; what_am_i_doing_wrong&#34;}();
编辑
SELECT post.* FROM post JOIN
follower ON post.owner_user_id = follower.user_id AND
follower.follower_id = "3" JOIN
user ON post.owner_user_id = user.id= "3"
答案 0 :(得分:2)
select post.* from
(select user_id from follower where follower_id = 3 union select 3) blah
join post on blah.user_id = post.owner_user_id;
答案 1 :(得分:1)
试试这个:
SELECT Distinct post.* FROM post JOIN
follower ON post.owner_user_id = follower.user_id JOIN
user ON follower.user_id = user.id WHERE user.id = 3
你没有在JOIN语句中使用条件,而是使用WHERE。