用于检索上一个报表id的Laravel查询
使用此查询,我将获取第一个报告id,但我希望检索最新的服务报告id 提前谢谢用于检索上一个报表id的Laravel查询,laravel,laravel-5.2,Laravel,Laravel 5.2,使用此查询,我将获取第一个报告id,但我希望检索最新的服务报告id 提前谢谢 CustomerRequest::where('escalated', '=', 0) ->whereIn('customer_request.complain_status_id', array(9, 10)) ->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->orderBy('service_report_mapping.report_date', 'DESC')
->groupBy('customer_request.id')
->get();
输出:
服务报告id
:
20903
服务报告ID:20903 47185 87609 98661 98662
98662是我的最新报告id。请检查:
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->orderBy('service_report_mapping.id', 'DESC')
->groupBy('customer_request.id')
->get();
选中此项:
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->orderBy('service_report_mapping.id', 'DESC')
->groupBy('customer_request.id')
->get();
service\u report\u mapping.id
带有DESC
,第一个结果将是最后一个id:
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->orderBy('service_report_mapping.id', 'DESC')
->groupBy('customer_request.id')
->get();
service\u report\u mapping.id
带有DESC
,第一个结果将是最后一个id:
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->orderBy('service_report_mapping.id', 'DESC')
->groupBy('customer_request.id')
->get();
你应该试试这个:
只需将orderBy
放在groupBy
之后,然后进行如下检查:
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->groupBy('customer_request.id')
->orderBy('service_report_mapping.report_date', 'DESC')
->get();
或
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->groupBy('customer_request.id')
->orderBy('service_report_mapping.id', 'DESC')
->get();
你应该试试这个:
只需将orderBy
放在groupBy
之后,然后进行如下检查:
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->groupBy('customer_request.id')
->orderBy('service_report_mapping.report_date', 'DESC')
->get();
或
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->groupBy('customer_request.id')
->orderBy('service_report_mapping.id', 'DESC')
->get();
提供的信息不多…你试过了吗
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->latest();
?
顺便说一句,你的groupBy背后的最初想法是什么?似乎没有必要吗?提供的信息不多…你试过了吗
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('service_report_mapping.report_id')
->latest();
?
顺便说一句,你的groupBy背后的最初想法是什么?看起来没必要吗?我找到了解决办法。使用
max()
谢谢大家的回答
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('max(service_report_mapping.report_id)')
->orderBy('service_report_mapping.report_date', 'DESC')
->groupBy('customer_request.id')
->get();
我找到了解决办法。使用max()
谢谢大家的回答
CustomerRequest::where('escalated', '=', 0)
->whereIn('customer_request.complain_status_id', array(9, 10))
->leftjoin('service_report_mapping', 'service_report_mapping.complain_id', '=', 'customer_request.id')
->select('max(service_report_mapping.report_id)')
->orderBy('service_report_mapping.report_date', 'DESC')
->groupBy('customer_request.id')
->get();
您是否正在尝试获取客户请求?还是报告id?我要最后一个报告id。您的报告表是什么?服务报告映射表包含报告id、报告日期。您是否正在尝试获取客户请求?或者报表id?我要最后一个报表id。您的报表表是什么?服务报表映射表包含报表id、报表日期。我仍然要获取第一个报表id。您的表和列没有任何合理的名称。请给我们一些关于表架构和字段及其关系的信息。thankscustomer request table having complaints and service report mapping包含报告id,这些报告是针对投诉生成的。我的意思是,最好使用表信息(如字段及其类型)更新您的问题。但我仍然获得第一个报告id。您的表和列没有任何合理的名称。请给我们一些关于表架构和字段及其关系的信息。thankscustomer请求表包含投诉,服务报告映射包含报告ID,这些报告是针对投诉生成的。我的意思是,最好使用表信息(如字段及其类型)更新您的问题。@Nikita请检查我的答案,此答案已测试感谢帮助。但我的问题是,一个客户可以有多个报告,我希望每个客户使用它的最后一个报告id,并为所有客户排序。@Nikita请显示此报告的输出query@Nikita所以我会更了解你的问题,尽快给你答案reply@Nikita请检查我的答案,此答案已测试感谢帮助。但我的问题是,一个客户可以有多个报告,我希望每个客户使用它的最后一个报告id,并为所有客户排序。@Nikita请显示此报告的输出query@Nikita因此,我会更了解您的问题,并给您快速回复。通过groupBy,我只获得了一次客户详细信息,但一个客户有多份报告因此,我想对单个客户的报告进行排序,并从排序后的报告中获取最新的报告。使用groupBy,我只获得一次客户详细信息,但一个客户有多个报告,因此我想对单个客户的报告进行排序,并从排序后的报告中获取最新的报告。