用于照片共享社交应用程序的Mysql查询字符串,如Instagram

时间:2015-10-19 05:36:14

标签: 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
-------------------
 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"列是执行以下操作的用户的ID,而"target"列是要关注的用户的ID。

我目前在撰写查询字符串以解除USERX的照片时遇到问题,其他用户USERX的照片正在关注,并按照" photoid"在activities_stream_tbl和ORDER BY" date" activities_stream_tbl。

如果有人能帮助我,我会很高兴,向我展示一种更好的构建db的方法谢谢。

1 个答案:

答案 0 :(得分:1)

要拉取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')

如果您愿意,可以将上述两个结果合并。

例如:

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