如何在laravel中创建TEMPORARY表

时间:2017-03-02 12:26:16

标签: php mysql laravel-5

如何在laravel中创建TEMPORARY表,插入记录并检索 您好,我正在尝试在laravel中创建临时表并插入记录并从临时表中检索该记录,然后删除表。

但我的临时表不是创建

DB::raw(“CREATE TEMPORARY TABLE tbl_temp(temp_id VARCHAR(100),tempcolumn1 VARCHAR(100),tempcolumn2 VARCHAR(100),tempcolumn3 VARCHAR(100)) ;

3 个答案:

答案 0 :(得分:11)

试试这个

// CREATE TEMPORARY TABLE

$productList = DB::insert( DB::raw( "CREATE TEMPORARY TABLE tempproducts") );

// DELETE TEMPORARY TABLE

$dropTable = DB::unprepared( DB::raw( "DROP TEMPORARY TABLE tempproducts" ) );

答案 1 :(得分:1)

我最近创建了一个临时表(laravel 8):

public function createLocalStoreProductTable(): \Illuminate\Http\JsonResponse
    {
        $tempTable = env("DB_TEMP_STORE_PRODUCT_TABLE_NAME");
        DB::connection('mysql')->statement(
            "CREATE TABLE " . $tempTable . " (
                    `uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
                    `store_uid` int(10) unsigned NOT NULL)
                     PRIMARY KEY (`uid`),
                     KEY `store_uid` (`store_uid`)
                     ) ENGINE=InnoDB AUTO_INCREMENT=3035849 DEFAULT 
                       CHARSET=utf8;"

 return response()->json(array('success' => true), Response::HTTP_OK);

答案 2 :(得分:-1)

使用Schema,您可以创建临时表...

 Schema::create('temp', function (Blueprint $table) {
                $table->increments('id');
                $table->string('session_id');

            });