我有一个带有照片表(包含照片)的MySQL数据库和一个跟随表(包含关于用户跟随用户的信息)。
我有以下代码来加载当前用户所关注的用户的照片:
SELECT photos.* FROM photos,follows
WHERE follows.following = photos.uid AND
follows.follower=$current_uid
ORDER BY photos.id DESC
我想加载用户上传的照片以及用户关注的照片列表。我尝试了以下内容,但我不断收到大量重复的条目:
SELECT photos.*
FROM photos,follows
WHERE (follows.following = photos.uid AND
follows.follower=$current_uid) OR
photos.uid=$current_uid
ORDER BY photos.id DESC
任何人都可以帮忙吗?
答案 0 :(得分:0)
试试这个:
SELECT
photos.*
FROM
photos
LEFT OUTER JOIN
follows
ON
photos.uid = follows.following AND follows.follower = $current_uid
WHERE
photos.uid = $current_uid
OR
follows.following IS NOT NULL
可替换地:
SELECT
photos.*
FROM
photos
WHERE
photos.uid = $current_uid
OR
photos.uid IN (SELECT following FROM follows WHERE follower = $current_uid)