为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕田峪9158850 2023-11-03 16:01:58
如何使用一个 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 选择并合并数组,但是很难订购
查看完整描述

2 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

使用whereIn,它是专门为您的目的而构建的:

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


查看完整回答
反对 回复 2023-11-03
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

您需要使用whereIn而不是where

->whereIn('author', $array)

现在您的查询如下所示:

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


查看完整回答
反对 回复 2023-11-03
  • 2 回答
  • 0 关注
  • 94 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信