2015-10-19 51 views
1

我正在开发照片共享应用程序平台。该应用程序允许您发布照片,其他人可以喜欢或评分照片。用户可以相互关注,看到他们的“追随”照片正在分享,就像Instagram一样。像照片共享社交应用程序的Instagram查询字符串,如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_table保持有用户的基本信息,而#photo_tbl持有的名称和用户共享照片的路径。在#friendship_tbl是用户之间的关系链接。 "actor"列是执行以下操作的用户的标识,而"target"列是要跟踪的用户的标识。

我目前有问题写一个查询字符串来拉USERX照片和其他用户USERX的照片是继和GROUP他们通过在activities_stream_tbl和ORDER BY“日期” activities_stream_tbl“PHOTOID”。

我会很高兴,如果有人能帮助我,告诉我结构分贝的更好的方法谢谢。

回答

1

拉用户X的照片,你可以构造你的SQL像

select PATH 
from user_tbl as a inner join photo_tbl as b 
on a.id = b.user_id 
and a.name = 'userx' 

,并拉动用户X跟随其他用户的照片,你可以写

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') 

可以联合上述两个结果如果你想。

例如:

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') 
+0

谢谢@ PK20,我怎样才能把两个查询到一个查询字符串。 –

+0

只是使用中间的联合。更新了我的答案。 – PK20