gpt4 book ai didi

Laravel 4 - 加入表但只有最新行

转载 作者:行者123 更新时间:2023-12-01 00:41:17 27 4
gpt4 key购买 nike

我有 2 个表:报价、回复。

在数据表中,我显示引号,并且对于每一行,我显示响应的列数。我现在想显示最新回复的日期,但我被阻止了:(

这是我的代码:

$quotes = Quote::select('Quotes.*', DB::raw('COUNT(Responses.id) as total_responses') )
->leftjoin('Responses', function($join)
{
$join->on('Responses.quote_id', '=', 'Quotes.id')
->where('Responses.state', '!=', '0');
})
->groupby('Quotes.id');

是否可以使用同一个表添加另一个左连接,使用自定义查询仅选择最新行?

谢谢你的帮助

最佳答案

您可以使用原始 MAX功能。假设您的日期已存储 Responses.date ,代码看起来像这样

$quotes = Quote::select('Quotes.*', DB::raw('COUNT(Responses.id) as total_responses'), DB::raw('MAX(CAST(Responses.date AS CHAR)) as latest') )
->leftjoin('Responses', function($join) {
$join->on('Responses.quote_id', '=', 'Quotes.id')
->where('Responses.state', '!=', '0');
})
->groupby('Quotes.id');

由于 mysql 错误 https://bugs.mysql.com/bug.php?id=54784 首先必须将其转换为字符

关于Laravel 4 - 加入表但只有最新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37642825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com