在过去的几天里,我一直在尝试创建一个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
答案 0 :(得分: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
答案 1 :(得分: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 Fiddle