简体   繁体   English

如何在 laravel 中创建 TEMPORARY 表

[英]How to create TEMPORARY table in laravel

how to create a TEMPORARY table in laravel, insert a record and retrieve hello, I'm trying to create a temp table in laravel and insert a record and retrieve that record from temp table and then drop the table.如何在 laravel 中创建一个临时表,插入一条记录并检索 你好,我正在尝试在 laravel 中创建一个临时表并插入一条记录并从临时表中检索该记录,然后删除该表。

But my temp table is not created但是我的临时表没有创建

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

Try this试试这个

// CREATE TEMPORARY TABLE // 创建临时表

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

// DELETE TEMPORARY TABLE // 删除临时表

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

I have recently created a temp table( laravel 8) as such:我最近创建了一个临时表(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);

If you want a temporary table containing the result of a query:如果你想要一个包含查询结果的临时表:

DB::statement('CREATE TEMPORARY TABLE your_table_name SELECT ...insert query here');

If you want to define the table before inserting rows:如果要在插入行之前定义表:

DB::statement('CREATE TEMPORARY TABLE your_table_name (...insert normal DDL here)');

Then just insert rows as in a normal table:然后像在普通表中一样插入行:

DB::table('your_table_name')->insert(["column" => "value"]);

or get result as in normal table:或者像普通表一样得到结果:

DB::table('your_table_name')->get();

With Schema you can create temp tables...使用 Schema,您可以创建临时表...

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

            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM