我正在尝试按月过滤用户和朋友的帖子。
这是我的表的结构:
Friends
id username friend
1 kyle jan
2 jan kyle
3 kyle nic
4 nic kyle
Posts
id title username date
1 hello world kyle 2015-01-02
2 hola mundo jan 2015-02-02
3 como estas nic 2015-02-03
这是我到目前为止所尝试的:
SELECT DISTINCT(p.title), p.date, p.username
FROM posts AS p
LEFT JOIN friends AS f
ON
p.username = f.username
OR p.username = f.friend
WHERE p.username = 'kyle'
AND MONTH(p.date) = 1
OR f.username = 'kyle'
OR f.friend = 'kyle'
但结果仍然会获得所有帖子而不会被过滤。有任何想法吗?
更新: 请检查我的sqlfiddle http://www.sqlfiddle.com/#!2/9980d/2