본문 바로가기
MySql

MySQL Laravel에서 Insert ... Select 문 만들기

by 베이스 공부 2020. 12. 1.
반응형

이 쿼리를 Laravel 용으로 변환해야하는데 Laravel의 Eloquont ORM 또는 Queries를 사용하여 Insert ... Select 문을 만드는 데 문제가 있습니다. 이 쿼리를 만드는 방법을 잘 모르겠습니다.

Insert into Demand (Login, Name, ApptTime, Phone, Physician, Location, FormatName, FormatDate, FormatTime, ApptDate, FormatPhone, cellphone)
Select Login, Name, ApptTime, Phone, Physician, Location, FormatName, FormatDate, FormatTime, ApptDate, FormatPhone, cellphone from " . [dbname] . "
    Where " . $where_statement

Laravel의 ORM을 사용하여이 쿼리를 생성하는 방법은 무엇입니까?


 

해결 방법

 

하나의 쿼리로이 작업을 수행 할 수있는 방법은 없지만 (라 라벨 5.7을 사용하지 않는 한) 동일한 문제가 발생하여 QueryBuilder로 빌드 한 특정 선택 항목을 계속 사용할 수 있는지 확인하고 싶었습니다.

그래서 당신이 할 수있는 일은 절반을 깨끗하게 유지하고 이전에 select 문을 만든 기능을 재사용하기 위해 다음과 같습니다.

/**
 * Wherever your Select may come from
 **/
$select = User::where(...)
                  ->where(...)
                  ->whereIn(...)
                  ->select(array('email','moneyOwing'));
/**
 * get the binding parameters
 **/ 
$bindings = $select->getBindings();
/**
 * now go down to the "Network Layer"
 * and do a hard coded select, Laravel is a little
 * stupid here
 */
 $insertQuery = 'INSERT into user_debt_collection (email,dinero) '
                . $select->toSql();

 \DB::insert($insertQuery, $bindings);

라 라벨 5.7 업데이트


따라서 위의 쿼리는 다음과 같습니다.

DB::table('user_debt_collection')->insertUsing(['email','dinero'], $select);

 

참조 페이지 https://stackoverflow.com/questions/25533608

 

 

반응형

댓글