0

我有一个 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

有人能帮忙吗?

4

1 回答 1

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)
于 2012-08-22T01:17:25.090 回答