【问题标题】:group by result not exist in join other tabel laravel?加入另一个表laravel中不存在按结果分组?
【发布时间】:2021-10-15 17:47:03
【问题描述】:

我有两张表和一张数据透视表。 结算表是这样的:

        $table->ulid();
        $table->string('track_id', 36);
        $table->string('payee_user_id', 36);
        $table->string('terminal_type', 36)->nullable();
        $table->string('terminal_provider', 36)->nullable();
        $table->string('terminal_psp', 36)->nullable();
        $table->string('terminal_id', 36)->nullable();
        $table->string('acceptor_id', 36)->nullable();
        $table->string('terminal_no', 36)->nullable();
        $table->string('account_bank_id', 36)->nullable();
        $table->string('account_id', 36)->nullable();
        $table->string('account_deposit', 36)->nullable();
        $table->string('account_iban', 36)->nullable();
        $table->integer('count')->nullable();
        $table->decimal('amount_settle', 15, 2);
        $table->decimal('amount_wage', 15, 2);
        $table->timestamps(6);

事务表是这个

        $table->ulid();
        $table->string('payee_user_id', 36);
        $table->string('type', 36);
        $table->string('terminal_id', 36);
        $table->string('terminal_no', 36);
        $table->string('terminal_type', 36);
        $table->string('acceptor_id', 36);
        $table->string('terminal_psp', 36);
        $table->string('terminal_provider', 36);
        $table->decimal('amount_settle', 15, 2);
        $table->decimal('amount_wage', 15, 2);
        $table->string('account_id', 36)->nullable();;
        $table->string('account_bank_id', 36)->nullable();;
        $table->string('account_deposit', 26)->nullable();
        $table->string('account_iban', 26)->nullable();
        $table->string('wallet_id', 36)->nullable();
        $table->timestamp('paid_at', 6);

settlements_transactions 表是这样的:

        $table->string('transaction_id')->index();
        $table->string('settlement_id')->index();
        $table->foreign('transaction_id', 'fk_transaction_id')
            ->on('transactions')->references('id');

        $table->foreign('settlement_id', 'fk_settlement_id')
            ->on('settlements')->references('id');

如果我之前没有保存,我将在结算表中插入事务组。

$transactions = DB::table('transactions')
        ->where('paid_at', '<', $date)
        ->where('terminal_type', Transaction::PRIVATE)
        ->where('terminal_provider', Transaction::SHAPARAK)
        ->distinct()->selectRaw(
            'account_iban,
             account_deposit,
             account_bank_id,
             payee_user_id,
             account_id,
             terminal_psp,
             terminal_id,
             terminal_no,
             acceptor_id,
             account_id,
             SUM(amount_settle) as amount_settle,
             SUM(amount_wage) as amount_wage,
             COUNT(id) as count'
        )->groupBy(
            'payee_user_id',
            'account_id',
            'account_iban',
            'terminal_psp',
            'terminal_no',
            'terminal_id',
            'acceptor_id',
            'account_id',
            'account_deposit',
            'account_bank_id',
        )->orderBy('payee_user_id')
         ->orderBy('account_id')
         ->orderBy('terminal_psp')
        ->orderBy('terminal_id')
        ->orderBy('account_id')
        ->orderBy('account_deposit')
        ->orderBy('account_bank_id')
        ->orderBy('account_iban')
        ->orderBy('terminal_no')
        ->orderBy('acceptor_id')
        ->get();

      foreach ($transactions as $transaction) {

        $exist = DB::table('settlements')
            ->where('payee_user_id', $transaction->payee_user_id)
            ->where('account_id', $transaction->account_id)
            ->where('account_deposit', $transaction->account_deposit)
            ->where('terminal_id', $transaction->terminal_id)
            ->where('terminal_psp', $transaction->terminal_psp)
            ->where('account_bank_id', $transaction->account_bank_id)
            ->where('terminal_provider', Transaction::SHAPARAK)
            ->where('terminal_type', Transaction::PRIVATE)
            ->where('settlements.created_at', '>=', Carbon::today())
            ->join('settlement_statuses', 'settlement_statuses.settlement_id', 'settlements.id')
            ->applySettlementLastStatusSubJoin('settlement_statuses')
            ->applySettlementLastAccepted('settlements')
            ->exists();

        if ($exist) {
            continue;
        }

      //insert in settlements table

   }

     

我不想检查循环中是否存在。如果结算表中不存在,我需要按结果获取交易组。

【问题讨论】:

    标签: mysql laravel lumen


    【解决方案1】:

    当您调整交易选择以便无需验证结算中是否已存在条目时。原则是执行“左连接”,然后过滤在结算表中没有对应条目的值 (-&gt;whereNull('s.id'))。

    此解决方案的问题可能是性能,这取决于几个因素。无论如何,如果正确使用索引,这应该不是问题。

    
    $transactions = DB::table('transactions AS t')
        ->leftJoin('settlements AS s', function ($join) {
            $join
                ->on('s.payee_user_id', '=', 't.payee_user_id')
                ->on('s.account_id', '=', 't.account_id')
                ->on('s.account_deposit', '=', 't.account_deposit')
                ->on('s.terminal_id', '=', 't.terminal_id')
                ->on('s.terminal_psp', '=', 't.terminal_psp')
                ->on('s.account_bank_id', '=', 't.account_bank_id')
                ->where('s.terminal_provider', '=', Transaction::SHAPARAK)
                ->where('s.terminal_type', '=', Transaction::PRIVATE)
                ->where('s.created_at', '>=', Carbon::today());
        })
        ->whereNull('s.id')
        ->where('t.paid_at', '<', $date)
        ->where('t.terminal_type', Transaction::PRIVATE)
        ->where('t.terminal_provider', Transaction::SHAPARAK)
        ->distinct()->selectRaw(
            't.account_iban,
                 t.account_deposit,
                 t.account_bank_id,
                 t.payee_user_id,
                 t.account_id,
                 t.terminal_psp,
                 t.terminal_id,
                 t.terminal_no,
                 t.acceptor_id,
                 t.account_id,
                 SUM(t.amount_settle) as amount_settle,
                 SUM(t.amount_wage) as amount_wage,
                 COUNT(t.id) as count'
        )->groupBy(
            't.payee_user_id',
            't.account_id',
            't.account_iban',
            't.terminal_psp',
            't.terminal_no',
            't.terminal_id',
            't.acceptor_id',
            't.account_id',
            't.account_deposit',
            't.account_bank_id',
        )->orderBy('t.payee_user_id')
        ->orderBy('t.account_id')
        ->orderBy('t.terminal_psp')
        ->orderBy('t.terminal_id')
        ->orderBy('t.account_id')
        ->orderBy('t.account_deposit')
        ->orderBy('t.account_bank_id')
        ->orderBy('t.account_iban')
        ->orderBy('t.terminal_no')
        ->orderBy('t.acceptor_id')
        ->get();
    

    编辑: 相同的解决方案,但使用关系表..

    <?php
    
    $transactions = DB::table('transactions AS t')
        ->leftJoin('settlements_transactions AS st', function ($join) {
            $join
                ->on('st.transaction_id', '=', 't.id')
                // these "where" conditions are maybe redundant
                ->where('s.terminal_provider', '=', Transaction::SHAPARAK)
                ->where('s.terminal_type', '=', Transaction::PRIVATE)
                ->where('s.created_at', '>=', Carbon::today());
        })
        ->whereNull('st.transaction_id')
        ->where('t.paid_at', '<', $date)
        ->where('t.terminal_type', Transaction::PRIVATE)
        ->where('t.terminal_provider', Transaction::SHAPARAK)
        ->distinct()->selectRaw(
            't.account_iban,
                 t.account_deposit,
                 t.account_bank_id,
                 t.payee_user_id,
                 t.account_id,
                 t.terminal_psp,
                 t.terminal_id,
                 t.terminal_no,
                 t.acceptor_id,
                 t.account_id,
                 SUM(t.amount_settle) as amount_settle,
                 SUM(t.amount_wage) as amount_wage,
                 COUNT(t.id) as count'
        )->groupBy(
            't.payee_user_id',
            't.account_id',
            't.account_iban',
            't.terminal_psp',
            't.terminal_no',
            't.terminal_id',
            't.acceptor_id',
            't.account_id',
            't.account_deposit',
            't.account_bank_id',
        )->orderBy('t.payee_user_id')
        ->orderBy('t.account_id')
        ->orderBy('t.terminal_psp')
        ->orderBy('t.terminal_id')
        ->orderBy('t.account_id')
        ->orderBy('t.account_deposit')
        ->orderBy('t.account_bank_id')
        ->orderBy('t.account_iban')
        ->orderBy('t.terminal_no')
        ->orderBy('t.acceptor_id')
        ->get();
    

    【讨论】:

    • 我认为你的答案是正确的,但你忽略了settlements_transactions 表。
    • 有可能,我没有意识到“//在结算表中插入”还有一个关系插入。 “leftJoin”会更容易。我将编辑并更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多