laravel - 使用 Laravel 选择恰好一个月内最近的记录
问题描述
我正在加入 Laravel 中的两 (2) 个表 (users
和billings
)。在计费表中,可以对用户进行多次计费。因此,这意味着用户名可以出现多次。我想根据created_at
(日期)选择账单的最新记录,然后选择用户名和created_at
.
我在 Laravel 中创建了一个选择查询。
$monthlysubscriptionupdate = DB::table("billings")
->select("billings.phone", "billings.email", "billings.plan",
"billings.created_at", "users.username",
"users.msisdn", "users.auto_subscription")
->join("users", "users.username", "=", "billings.username")
->where(['users.package_id' => '1102', 'users.auto_subscription' => 0])
->get();
在计费表中,可以对用户进行多次计费。因此,这意味着用户名可以出现多次。我想根据created_at
(日期)选择账单的最新记录,并created_at
在当前日期和created_at
. 请注意,每个用户名都会出现多次。
解决方案
正如 Daniel Luna 所说,您可以使用 whereDate + orderBy 。whereDate 示例
$q->whereDate('created_at', '=', date('Y-m-d'));
无论如何,如果您在需要处理复杂查询的地方使用这种类型的数据库结构,我强烈建议您使用 Eloquent 和项目的面向对象结构。像这样你就可以避免查询,只要一个段落^_^ 喜欢这个。
推荐阅读
- linux - 根据bash中的位置排序
- ruby - 尝试添加 gem 设计时没有路线匹配 [GET] "/"
- kubernetes - 使用域套接字在 kubernetes pod 之间进行通信
- android - AAR 文件:检测 API 级别和编译它的 Java/JDK 版本
- flutter - 当我更改局部变量时,类最终参数正在更新
- encryption - 如何解密不使用密钥但字节的 aes128
- vue.js - Vue JS - 制作一个类似于浏览器的 ctrl + f 的功能(带滚动)
- google-analytics - Google Analytics(分析)仪表板上没有显示自定义事件
- bash - 更改输出文件名的 Bash Loop
- vue.js - Vue.js x-template 运行良好,但在 IDE 中无法解析