php - 在 Laravel 中检查重复数据

标签 php mysql laravel laravel-5

此代码的工作原理是从 ext_db 发送到 inn_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]);
            }
        });
    }
}

最后,经过讨论,我得到了以下带有连接 View 的代码的答案。

感谢@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);

最佳答案

试一试,您基本上需要检查 customer 表中 block 的每条记录,如果它不存在,然后允许它们插入到 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]);
                  }
            }
        } );
    }

关于php - 在 Laravel 中检查重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46468210/

相关文章:

javascript - Jquery/PHP : passing JSON data with Ajax ends up truncated

php - 来自原始查询的 laravel 查询生成器

php - 在 PHP 中使用日期时间格式

php - Laravel 5 内置用户身份验证中的重定向

mysql - 消息发件人表的最佳查询变体

php - 根据条件禁用按钮

php - pdo 按相关性排序,然后按另一个字段排序

mysql - Laravel - 数据库事务 - 超出锁定等待超时

php - 带外键的 Laravel 存储模型

javascript - Laravel 网站的站点地图,其中包含变量