1

在过去的几天里,我一直在尝试创建一个 SQL 查询来显示朋友之间的帖子。但是,我无法弄清楚我做错了什么。对我来说,这些帖子似乎在重复,并且表现与我的意图不同。

此查询“应该”显示来自我和与我成为朋友的用户的帖子。

SELECT *
FROM bhost_entries
WHERE
  author_u_id='$user_info[u_id]'
  OR author_u_id IN (SELECT follower_u_id
                     FROM bhost_userfollow
                     WHERE following_u_id='$user_info[u_id]')
ORDER BY e_id DESC

表:

bhost_entries:     e_id, author_u_id, title
bhost_userfollow:  uf_id, following_u_id, follower_u_id

我完全偏离轨道了吗?欣赏任何想法。

输出(当跟随 2 个用户时)

Heihei
Aker Brygge-spesial
Vi har oppdatert retningslinjene
Hei, dette er en kladd!
To do list
Testinnlegg
Push Notifications
Californication
Hank Moody
Hei bloggen
Heihei
Aker Brygge-spesial
Vi har oppdatert retningslinjene
Hei, dette er en kladd!
To do list
Testinnlegg
Push Notifications
Californication
Hank Moody
Hei bloggen
4

2 回答 2

0

如果我可以测试它,我会这样做:

SELECT e_id, author_u_id, title
FROM bhost_entries, bhost_userfollow
WHERE
  author_u_id='$user_info[u_id]'
  OR (follower_u_id='$user_info[u_id]' AND following_u_id=author_u_id)
ORDER BY e_id DESC
GROUP BY e_id
于 2013-04-10T11:29:46.477 回答
0

你可以尝试这样的事情:

SELECT DISTINCT (e.e_id) AS entry_id,
       e.title AS entry_title, 
       e.author_u_id AS author_id
FROM bhost_entries AS e
JOIN bhost_userfollow AS f
ON (f.following_u_id = e.author_u_id) 
WHERE (e.author_u_id = '$user_info[u_id]' OR f.follower_u_id = '$user_info[u_id]')

如果我理解正确,它就可以了。在这里查看:SQL 小提琴

于 2013-04-11T07:37:15.803 回答