laravel - 如何使用 Laravel Query Builder 获取最后更新的记录?
问题描述
我正在使用 Laravel Query Builder 从数据库中获取最后更新的记录。但是,它显示不同的id
不同是正确的,它显示了我上次更新的日期,但每次我得到第一个update_date.
update_date
id.
$results = DB::table('customer_info as cust_info')
->join('client_emp as client_info', 'cust_info.id', '=', 'client_info.cust_id')
->select('cust_info.id', DB::raw('MAX(cust_info.updated_at)'))
->orderBy('cust_info.id','DESC')
->first();
在上面的查询中,我编写了 select 来获取id
最后更新的记录。
->select('cust_info.id', DB::raw('MAX(cust_info.updated_at)'))
但是,它updated_date
每次只向我显示最后一个表中的第一个 id。
解决方案
尝试这个:
orderBy('updated_at','DESC')->first()->id
在这里,您将获得最新更新记录的 id。当您删除 first() 方法时,您将获得按“updated_at”排序的整个集合
推荐阅读
- python-2.7 - 如何使用bs4登录
- eclipse - Eclipse:导入 org.apache.tomcat 无法解析
- reactjs - ReactJS错误TypeError:无法读取未定义的属性“地图”
- javascript - 在变量中存储返回值与按名称调用函数不同
- javascript - 如何在编辑器中输入文字?
- jenkins - 如何使用 fastlane 和 jenkins 为 iOS CICD 设置增加内部版本号?
- scala - 使用 spark 从大型 hdfs 目录中读取几行的最快方法是什么?
- hive - 在 Hive 中选择 database.table.column
- python-3.x - 将输入写入文件,不包括最后一行
- apache - JAX-WS 桌面打开文件