2015-10-19 52 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