Php 照片共享社交应用程序(如Instagram)的Mysql查询字符串

Php 照片共享社交应用程序(如Instagram)的Mysql查询字符串,php,mysql,sql,sharing,Php,Mysql,Sql,Sharing,我正在开发一个照片共享应用程序平台。该应用程序允许你发布照片,其他人可以喜欢或评价照片。用户可以像instagram一样,互相跟踪并查看他们的“追随者”正在共享的照片 #user_tbl id | name | number ------------------- 1 | Dan | 0209 2 | Sam | 2854 3 | Dave | 8123 4 | Alex | 5600 #photo_tbl id | userid | path -----------------

我正在开发一个照片共享应用程序平台。该应用程序允许你发布照片,其他人可以喜欢或评价照片。用户可以像instagram一样,互相跟踪并查看他们的“追随者”正在共享的照片

 #user_tbl
id | name | number
-------------------
 1 | Dan  | 0209
 2 | Sam  | 2854
 3 | Dave | 8123
 4 | Alex | 5600

#photo_tbl
id | userid | path
-------------------
 1 |  3     | dave-dog.jpg
 2 |  1     | dans-cat.png
 3 |  4     | alex-bird.jpg
 4 |  2     | sam-fish.jpg


#friendship_tbl
id | actor | target
--------------------
 1 |  2    | 1   // Sam is following Sam
 2 |  2    | 4   // Sam is following Alex
 3 |  1    | 3   // Dan is following Dave
 4 |  4    | 2   // Alex is following Sam

 #activities_stream_tbl
id | photoid | userid | context               | date
----------------------------------------------------------
 1 |   3     |  4     | add-new-photo         | 10/10/2015
 2 |   1     |  3     | add-new-photo         | 12/10/2015
 3 |   3     |  2     | Sam-share-Alex-photo  | 15/10/2015
 4 |   4     |  2     | add-new-photo         | 20/10/2015
 6 |   1     |  1     | Dan-like-Dave-photo   | 21/10/2015
#user_表保存用户的基本信息,
#photo_tbl
保存用户共享的照片的名称和路径。在
#中,友谊tbl
是用户之间的关系链接
“actor”
列是执行以下操作的用户的id,
“target”
列是被跟踪用户的id

我目前在编写查询字符串以提取
USERX
的照片时遇到问题,其他用户的照片
USERX
正在跟踪并按活动流中的“photoid”对其分组,并按“日期”活动流排序


如果有人能帮助我,我会很高兴的,告诉我一个更好的构造数据库的方法。谢谢。

要获取USERX的照片,你可以像sql一样构造你的数据库

select PATH
from user_tbl as a inner join photo_tbl as b 
on a.id = b.user_id
and a.name = 'userx'
要获取USERX下面其他用户的照片,您可以编写

select path
from photo_tbl as a
where a.userid in (select target from friendship_tbl as x inner join user_tbl as y on x.actor = y.id and y.name = 'user')
如果需要,可以合并上述两个结果

例:


谢谢@PK20,我如何将这两个查询放在一个查询字符串中。
select PATH
from user_tbl as a inner join photo_tbl as b 
on a.id = b.user_id
and a.name = 'userx'
   UNION
select path
from photo_tbl as a
where a.userid in (select target 
                   from   friendship_tbl as x 
                   inner  join user_tbl as y 
                     on   x.actor = y.id and y.name = 'user')