使用Join作为“选择”正确语法的Laravel QB更新是什么?

时间:2019-04-05 10:01:39

标签: php laravel

我有正确的MySQL查询,可以很好地工作,我试图将其推入Laravel Query Builder语法,但不了解其中的一小部分。

我试图编写QB表达式(见下文)。

这是我的SQL查询:

    update LOW_PRIORITY zoho_contacts z
    inner join
    (select contactid,  min(due_date) as mini_date
    from zoho_invoices
    where deleted_at is null and due_date is not null and summa_oplaty > 0
    group by contactid ) co using (contactid)
    set z.data_pervoy_pokupki = co.mini_date
    where z.contactid = co.contactid AND z.data_pervoy_pokupki IS null

这是我的QB查询:

        DB::table('zoho_contacts z')
            ->join(DB::table('zoho_invoices')
            ->select(DB::raw('contactid,  min(due_date) as mini_date'))
            ->whereNull('deleted_at')
            ->whereNotNull('due_date')
            ->where('summa_oplaty', '>', 0)
            ->groupBy('contactid') . ' as co', 'z.contactid', 'co.contactid' )
        ->update(['z.data_pervoy_pokupki' => 'co.mini_date'])
        ->where('z.contactid', 'co.contactid')
        ->whereNull('z.data_pervoy_pokupki');

        dd(DB::getQueryLog());

这是我的表情产生错误

Object of class Illuminate\Database\Query\Builder could not be converted to string

指向字符串

->groupBy('contactid') . ' as co', 'z.contactid', 'co.contactid' )

我建议错误为'as co',因为它放置在不合适的位置。 我需要这个别名,因为我有这个

->where('z.contactid', 'co.contactid')

如何命名嵌套在Join中的查询结果?

->select(DB::raw('contactid,  min(due_date) as mini_date'))
    ->whereNull('deleted_at')
    ->whereNotNull('due_date')
    ->where('summa_oplaty', '>', 0)
    ->groupBy('contactid') . ' as co', 'z.contactid', 'co.contactid'

1 个答案:

答案 0 :(得分:0)

检查是否可行:

  DB::table('zoho_contacts z')
        ->join(DB::raw("(select contactid,  min(due_date) as mini_date from zoho_invoices
        where deleted_at is null and due_date is not null and summa_oplaty > 0
        group by contactid ) co"),"z.contactid'","=","co.contactid")
        ->where('z.contactid', 'co.contactid')
        ->whereNull('z.data_pervoy_pokupki')
        ->update(['z.data_pervoy_pokupki' => 'co.mini_date']) ;