Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在Laravel 5.3中使用SQL变量?_Mysql_Variables_Batch Processing_Laravel 5.3 - Fatal编程技术网

Mysql 如何在Laravel 5.3中使用SQL变量?

Mysql 如何在Laravel 5.3中使用SQL变量?,mysql,variables,batch-processing,laravel-5.3,Mysql,Variables,Batch Processing,Laravel 5.3,我正在尝试执行批处理查询,示例如下: foreach ($getRecordsFromAllTempTable as $key => $value) { $studentId = $value["csv_student_id"]; $csvDescription = $value["csv_desc"]; $csvAmount = $value["csv_amount"]; $transaction_category = $value["csv_cat_id"]; $transa

我正在尝试执行批处理查询,示例如下:

    foreach ($getRecordsFromAllTempTable as $key => $value) {

$studentId = $value["csv_student_id"];
$csvDescription = $value["csv_desc"];
$csvAmount = $value["csv_amount"];
$transaction_category = $value["csv_cat_id"];
$transaction_type = $value["csv_transaction_type"];
$batch_desc = $value["batch_desc"];

        $batchSqlQuery .= "

            INSERT INTO ". StudentRegisterTransaction::TABLE ." 

            (student_id,transaction_type, transaction_category, description, amount, created_time, created_date, created_by)

            VALUES 

            ($studentId, '". $transaction_type ."',$transaction_category, '". $csvDescription ."', '". $csvAmount ."', 
            '".  $datetime ."', '". $date ."',".session('userid').");

            set @lastid = LAST_INSERT_ID();

            INSERT INTO ". StudentRegisterTransactionUploadLog::TABLE ." (transaction_id, student_id, batch_id, amount, description,  
            payment_category, created_by ,authorized_by, created_time) 

            VALUES 

            (@lastid, $studentId, '". $batchId ."', '". $csvAmount ."', '".$batch_desc."', $transaction_category, ". session('userid') .",
             '". session('email') ."', '". $datetime ."');


             DELETE FROM ". studentregisterTransactionTempLog::TABLE ." WHERE id=" . $value['id'] . ";


            ";
    }

            \DB::insert(\DB::raw($batchSqlQuery)); 
但当我这样做时,我会得到以下错误:

[2017-01-29 21:35:33] 173.194.83.243.ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set @lastid = LAST_INSERT_ID();

看起来Laravel无法理解MYSQL变量,或者我做错了什么。但是,如果我直接在数据库中运行查询,它可以正常工作

你在用拉威尔。为什么要编写如此低级的SQL代码,更重要的是,为什么要在不转义的情况下编写呢?使用准备好的语句。我想对多个表使用带有大量insert语句的批处理查询。您在这里进行了过度优化,把情况弄得一团糟<代码>插入只接受一条语句和一条语句。您在这里没有给出太多的上下文,但我打赌您可以使用
INSERT INTO x SELECT。。。从y开始
如果您进行了操作。这里的问题在于**set@lastid=LAST\u INSERT\u ID();**SQL语句,给定SQL;你有没有别的建议?基本上,我想得到插入的ID,如果我试图通过应用程序进行,因为大量的查询数据库通信需要很长时间。这整个事情需要重新思考。你不是在这里批量插入,你在运行大量的语句,所有语句都被弄乱了。如果你真的在做一个正确的多重插入,我可能会更支持这种方法,但你不是。查看使用插入到中的
的方法。。。从…
方法中选择,您可以在其中将数据从一个表插入到另一个表中,并根据需要进行转换。