[英]How to get right rows according to related table containing at least one element of an array?
I have posts
and postWriters
tables with one-to-many relationships. 我有具有一对多关系的
posts
和postWriters
表。 (I have also writers
and follows
tables). (我也有
writers
, follows
表格)。
Each post has been written by several writers collaboratively. 每个帖子都是由多位作者共同撰写的。
I want to get last 20 submitted posts which have been written by at least one writer I follow. 我想获得至少20位我所关注的作家所写的最近提交的帖子。
For example the writers that I follow: 例如,我关注的作家:
$arrayOfWriterIds_IFollow = [3, 5, 123, 45, ..., 3456] // total 100 ids
I want to get last 20 posts, at least one of the writers I follow contributed to them. 我想获得最近的20个帖子,至少我追踪的一位作家对此做出了贡献。
Which mysql query gives me true results? 哪个mysql查询给我真实的结果? Thanks.
谢谢。
Without knowing the full table structure, probably something like: 不知道完整的表结构,可能类似于:
SELECT * FROM postWriters LEFT JOIN posts ON postWriters.post_id=posts.id WHERE postWriter.writer_id IN (your, followed, writers, here) GROUP BY posts.id ORDER BY posts.date DESC LIMIT 0,20;
Using IN to select your writers, GROUP BY to remove duplicate posts then ORDER BY DESC to get the latest by date and finally limit. 使用IN选择您的作者,使用GROUP BY删除重复的帖子,然后使用ORDER BY DESC获取最新的日期和最终限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.