简体   繁体   English

如何根据包含至少一个数组元素的相关表获取正确的行?

[英]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. 我有具有一对多关系的postspostWriters表。 (I have also writers and follows tables). (我也有writersfollows表格)。

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.

相关问题 如何获取包含与Bllim Datatable的另一个表相关的行的数组-Laravel - How to get array with rows related to another table for Bllim Datatable - Laravel 如何测试数组是否包含至少一个元素 - How to test if array contains at least one element 使用具有关系的表获取一个表中的相关行 - Get related rows in one table using a table with relations 如何根据另一个元素的条件更新数组的一个元素? - How to update one element of an array according to a condition on another element? 在包含关联和索引级别的多维数组中搜索一个值,然后获取相关元素的值 - Search multidimensional array containing associative and indexed levels for a value then get a related element's value Laravel 验证至少需要数组中的一个元素 - Laravel validating required at least one element in array PHP-如何检查第二个数组中是否存在第一个数组的至少一个元素 - php - how to check if at least one element of first array exists in second array 如何从包含该对象的数组中获取对象的元素 - How to get an element of an object from an array containing the object 如何从一个pdoQuery中的两个相关表和一个不相关的表中获得多计数? - how get multi count from two related table and one not related in one pdoQuery? PHP if语句:如果一个元素至少等于数组中的一个元素 - PHP if statement: if an element equals at least one element from array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM