SQL查询返回从数组中选择作者的数据

如何使用一个 SQL 查询返回两个或多个不同作者的帖子?作者数组是从数据库接收的,对于每个用户来说都是不同的(这取决于您关注的作者)。


我返回$array带有作者 ID 的变量。


$array = array(15, 12, 18); //this array is returned from database and is always different

$posts = DB::table('posts') -> where('author', $array]) -> orderBy('date', 'DESC') -> get();


foreach($posts as $post) {

  echo "Posted by " . $post->author;

}

如何使用单行返回作者 15、12、18 发布的所有帖子或仅返回单 sql 查询并按日期排序?


我尝试为每个创建者进行不同的 sql 选择并合并数组,但是很难订购


慕田峪9158850
浏览 97回答 2
2回答

BIG阳

使用whereIn,它是专门为您的目的而构建的:$posts = DB::table('posts')      -> whereIn('author', $array)      -> orderBy('date', 'DESC')      -> get();

阿波罗的战车

您需要使用whereIn而不是where:->whereIn('author', $array)现在您的查询如下所示:$posts = DB::table('posts')->whereIn('author', $array)-> orderBy('date', 'DESC')->get();
打开App,查看更多内容
随时随地看视频慕课网APP