检查Laravel中的重复数据

时间:2017-09-28 11:39:04

标签: php mysql laravel-5

此代码用作从inn_db发送到ext_db表。

但无法检查inn_db中的数据是相同还是不同。

因此在inn_db中提出了相同的值。

我怎么能添加那份工作?。

Laravel-5.4,MySQL,InnoDB

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \DB;

class UpdateCustomerController extends Controller
{
    public function db_update()
    {
        $customers = \DB::connection('ext_db')->table('customers')->orderBy('customer_id')->chunk(1000, function ($all){
            foreach ($all as $kunde){
                DB::connection('inn_db')->table('custoemrs')->insert(
                    [$kunde->customer_id
                     $kunde->name,
                     $kunde->email]);
            }
        });
    }
}

最后,我在讨论后通过连接视图得到了以下代码的答案。

感谢@Pramid和@Badea:)

    $customers = \DB::connection('ext_db')->table('customers')->orderBy('customer_id')->chunk(1000, function ($all){
        foreach ($all as $kunde){
            $existing_kunde = DB::connection('inn_db')->table('customers')->where([
                    ['customer_id', '=', $kunde->customer_id], 
                    ['name', '=',  $kunde->name], 
                    ['email', '=', $kunde->email]
            ])->first();

            if ( ! $existing_kunde) {
                DB::connection('inn_db')->table('customers')->insert([
                    'customer_id' => $kunde->customer_id, 
                    'name', => $kunde->name, 
                    'email', => $kunde->email
                ]);
           }
        }
    });
    $kundes = \DB::connection('ext_db')->table('customers')->get();
    return view('kundes.index')
        ->with('kundes', $kundes);

2 个答案:

答案 0 :(得分:1)

是的,您需要在插入客户之前进行检查。只需添加一个像bellow一样的条件:

foreach ($all as $kunde){
    $existing_kunde = DB::connection('inn_db')->table('custoemrs')->where('customer_id', $kunde->customer_id)->first();
    if ( ! $existing_kunde) {
        DB::connection('inn_db')->table('custoemrs')->insert(
            [$kunde->customer_id
            $kunde->name,
            $kunde->email]);
   }
}

答案 1 :(得分:1)

尝试一下,你基本上需要检查customer表中每个块的记录(如果它不存在)然后允许它们插入customer

public function db_update()
    {
        $customers = \DB::connection( 'ext_db' )->table( 'customers' )->orderBy( 'customer_id' )->chunk( 1000, function ( $all ) {
            foreach ( $all as $kunde ) {
                $kunde_exist = DB::connection( 'inn_db' )->table( 'customers' )
                                 ->where( [
                                     'customer_id' => $kunde->customer_id,
                                     'name'        => $kunde->name,
                                     'email'       => $kunde->email,
                                 ] )->first();
                if ( ! $kunde_exists ) {
                    DB::connection( 'inn_db' )->table( 'customers' )->insert(
                        [ $kunde->customer_id
                             $kunde->name,
                             $kunde->email]);
                  }
            }
        } );
    }