laravel+mysql:可以创建/更新临时表,但不能从以下位置选择:

lpwwtiir  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(420)

我可以创建一个临时表,但是当我尝试从中选择时,它会说该表不存在。我认为这是因为db::statement和db::select在某种程度上与数据库有一个不同的会话,但我不知道如何修复它。
如何在同一个脚本中创建临时表、插入临时表并从中进行选择?
下面是tinker的输出,它演示了问题:

DB::select("select count(*) from users"); //12
DB::statement("create temporary table tempUsers like users"); //true
DB::statement("insert into tempUsers (id,email) VALUES (1, 'joe@example.com')"); //true

在“选择”会话中似乎不存在

DB::select("select count(*) from tempUsers");

illuminate\database\queryexception,消息为“sqlstate[42s02]:找不到基表或视图:1146表“db.tempusers”不存在(sql:select count(*)from tempusers)'
仍然存在于“声明”会话中

DB::statement("insert into tempUsers (id,email) VALUES (2, 'bob@example.com')"); //true
DB::statement("insert into tempUsers (id,email) VALUES (1, 'joe@example.com')");

illuminate\database\queryexception,消息为'sqlstate[23000]:完整性约束冲突:1062键'primary'的重复条目'1'(sql:insert into tempusers(id,email)值(1,'bob@example.com'))'
我用同样的方法 $table->temporary()Blueprint ,即

Schema::create('tempTable',function ($table) {$table->increments('id');
    $table->timestamps();
    $table->temporary();
});

使用laravel5.4、mysql 5.6
当我连接到laravel外部的db时,我可以很好地操作和选择temp表

w8biq8rn

w8biq8rn1#

原来这个问题是由我们的读写器设置引起的。我们使用一个数据库进行写入,另一个用于读取。writer数据库被复制到read数据库,但是temp表没有被复制,因为它是一个temp表,并且被锁定到与writer的当前会话中。
如果使用laravel 5.5+,解决方案是 'sticky' => true 在数据库连接上。这使得写入程序在任何写入之后都被视为读取器,从而绕过复制延迟的问题。
由于我现在仍坚持使用laravel5.4,所以我的解决方案是创建一个标准表,但将其视为temp表,并将其放在方法的末尾。

DB::select("select count(*) from users"); //12

//use uniqid to ensure a unique tablename since it is now in the global namespace
$tableName=uniqid('tempUsers'); 

DB::unprepared("create table $tableName like users"); //true
DB::statement("insert into tempUsers (id,email) VALUES (1, 'joe@example.com')"); //true
DB::select("select count(*) from $tableName"); //1
//...do stuff with temp table
//...
Schema::drop($tableName);
// return
a64a0gku

a64a0gku2#

如果在存储过程中创建临时表,则一旦存储过程完成,它就会被销毁。
临时表的作用域很重要,仅对特定用户可见。临时表以英镑符号“#”或“@”开头,这很重要,因为您似乎错过了这一部分。
因此,如果您将迁移调整为:

Schema::create('#tmpTest', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
});

然后运行:

php artisan migrate:fresh

要在临时表中插入数据:
\db::语句(“insert into #tmpTest (id,name)值(1,'joe@example.com')");
要使用select获取这些数据,请执行以下操作:
$a=db::select('选择*自 #tmpTest '); dd($a);
结果是:

相关问题