我正在编写一个时间轴PHP脚本:
posts
= relations.friend
posts.user
users
= relations.friend
users.id
醇>
该脚本运行正常,并检索用户朋友发送的所有帖子。
SELECT relations.friend as uid, users.name, users.email, posts.id as pid, posts.text, posts.date
FROM `relations`
INNER JOIN `posts` ON relations.friend = posts.id
INNER JOIN `users` ON relations.friend = users.id
WHERE relations.user = 2
我需要在结果中添加用户自己的帖子,但我无法做到。我的表格如下:
帖子
----------------------------------------------------------
id text user status date
----------------------------------------------------------
1 Post 1 1 1 01-01-2014
2 Post 2 2 1 01-02-2014
3 Post 3 3 1 01-03-2014
4 Post 4 4 1 01-03-2014
5 Post 5 5 1 01-04-2014
关系
--------------------------------------------
id user friend status
--------------------------------------------
1 1 2 1
2 2 1 1
3 2 3 1
4 3 2 1
5 3 1 1
6 4 1 1
7 5 1 1
8 2 4 1
用户
--------------------------------------------
id name email
--------------------------------------------
1 Adam adam@mail.com
2 Bryan bryan@mail.com
3 Mark mark@mail.com
4 Yuri yuri@mail.com
5 Andrew andrew@mail.com
有什么想法吗?
答案 0 :(得分:1)
Just UNION the results
SELECT relations.friend as uid, users.name, users.email, posts.id as pid, posts.text, posts.date
FROM `relations`
INNER JOIN `posts` ON relations.friend = posts.id
INNER JOIN `users` ON relations.friend = users.id
WHERE relations.user = 2
UNION
SELECT users.id as uid, users.name, users.email, posts.id as pid, posts.text, posts.date
FROM `users`
INNER JOIN `posts` ON posts.user = users.id
WHERE users.id = 2