Laravel获取联接表上的最新行

时间:2019-12-19 06:49:59

标签: php laravel

我有这个查询

$schedules = DB::table('project_schedule_reimbursments')
->where('project_schedule_reimbursments.project_id', $id)
->join('costs', 'costs.id', '=', 'project_schedule_reimbursments.cost_id')
->join('general_statuses', 'general_statuses.reimbrse_id', '=', 'project_schedule_reimbursments.id')

// ->join('statuses', function ($join) {
//     $join->on('general_statuses.status_id', '=', 'statuses.id');
// })

->join('statuses', 'statuses.id', '=', 'general_statuses.status_id')
->select(
    'project_schedule_reimbursments.id as id',
    'project_schedule_reimbursments.description as description',
    'costs.short_description as cost_name',
    'project_schedule_reimbursments.amount as amount',
    'project_schedule_reimbursments.spent_date as spent_date',
    'project_schedule_reimbursments.created_at as created_at',
    'statuses.name as statusName', // this supposed to return latest row
    DB::raw('max(general_statuses.created_at) as statusDate') // this is fixed now
)
->groupby('project_schedule_reimbursments.project_id')
->get();

这是我的数据库屏幕截图:

one

此查询中我需要的是从屏幕快照中获取最新状态详细信息row 8,而不是从row 1获取详细信息,这意味着第一行而不是最新行。

  

问题是:我在哪里犯了错误?该如何解决?

更新

这是我现在正在接收的数据

[
    {
        "id":17,
        "description":null,
        "cost_name":"Biaya Interview",
        "amount":null,
        "spent_date":"2019-12-18 00:00:00",
        "created_at":"2019-11-06 21:25:05",
        "statusName":"Rejected", // this is from row 1 (incorrect)
        "statusDate":"2019-12-20 09:14:46" // this is from row 8 (correct)
    }
]

更新2

逻辑

我需要获取无法使用limit(1)的数据数组

  1. 我选择项目
  2. 它返回20个结果
  3. 每个结果获取最新的状态名称和时间(每个结果有很多 状态,所以我只得到最新的)

更新3

感谢Zar Ni Ko Ko,它返回了最后一行,但是问题很小。

我添加了另一笔费用,因此当我选择项目时,我应该有2个结果,并且每个结果都具有最新状态(似乎是固定的)

two

因此,现在我得到id 17的报销,其最新状态来自此屏幕截图的第8行。但是id 20的报销没有回报。

2 个答案:

答案 0 :(得分:1)

尝试一下...

   $schedules = DB::table('project_schedule_reimbursments')
->where('project_schedule_reimbursments.project_id', $id)
->join('costs', 'costs.id', '=', 'project_schedule_reimbursments.cost_id')
->join('general_statuses', 'general_statuses.reimbrse_id', '=', 'project_schedule_reimbursments.id')

// ->join('statuses', function ($join) {
//     $join->on('general_statuses.status_id', '=', 'statuses.id');
// })

->join('statuses', 'statuses.id', '=', 'general_statuses.status_id')
->select(
    'project_schedule_reimbursments.id as id',
    'project_schedule_reimbursments.description as description',
    'costs.short_description as cost_name',
    'project_schedule_reimbursments.amount as amount',
    'project_schedule_reimbursments.spent_date as spent_date',
    'project_schedule_reimbursments.created_at as created_at',
    'statuses.name as statusName', // this supposed to return latest row
    DB::raw('max(general_statuses.created_at) as statusDate') // this is fixed now
)
->where('general_statuses.created_at', \DB::raw("(select max(`created_at`) from general_statues)"))
->groupby('project_schedule_reimbursments.project_id')
->get();

答案 1 :(得分:0)

尝试一下

orderBy('project_schedule_reimbursments.updated_at', 'desc')->limit(1)->get();