如何显示我的帖子以及关注者帖子MySQL

时间:2016-05-09 17:26:43

标签: php mysql sql mysqli

我试图创建一个像twitter这样的系统,用户可以相互连接。然后他们就能看到对方的帖子

这是用户表

+-----+------------+---------+
|  id |  name      |username |
+-----+------------+---------+
|   1 | Phillip    | user1   |
|   2 | Another    | user2   |
+-----+------------+---------+

连接表

+---------+-----------+--------------+--------------+
|      id | follower  |  following   |     status   |
+---------+-----------+--------------+--------------+             
|       1 |  user1    |  user2       |       0      |
|       2 |  user3    |  user1       |       1      |
+---------+-----------+--------------+--------------+

发表表格

+---------+-----------+--------------+--------------+
| post_id | content   |     title    |    username  |
+---------+-----------+--------------+--------------+             
|       1 | hello guyz|      eg1     |     user1    |
|       2 | example1  |      eg 2    |     user2    |
+---------+-----------+--------------+--------------+

查询我试过

SELECT
post.* 
FROM users
LEFT JOIN connection ON (users.username = connection.follower
 OR users.username = connection.following)
INNER JOIN messages ON (
  users.username = post.username
  OR connection.following = post.username
)
WHERE users.username = 'user1'
GROUP BY post.id
LIMIT 0, 8

此查询可能有什么问题,因为此查询也不完整

让您更好地理解 - >

假设我的名字是示例&如果我发送请求example2&他接受了我的请求status will be set to 1然后我怎样才能在我的时间轴上看到他的帖子&他怎么能在他的时间表上看到我的帖子

1 个答案:

答案 0 :(得分:0)

您想要的结果的查询是

SELECT * FROM `posts`
WHERE `username` = 'user1'
OR `username` IN (SELECT `follower` FROM `connection` WHERE `following`='user1' AND `status`=1)