【发布时间】:2018-08-13 18:30:03
【问题描述】:
我可以创建一个临时表,但是当我尝试从中选择时,它说该表不存在。我认为它是因为 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]: Base 未找到表或视图:1146 表 'db.tempUsers' 不存在(SQL: 从 tempUsers 中选择 count(*)'
仍然存在于“声明”会话中
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 键的重复条目“1” 'PRIMARY' (SQL: INSERT INTO tempUsers (id,email) VALUES (1,'bob@example.com'))'
我使用$table->temporary() 和Blueprint 得到相同的行为,即
Schema::create('tempTable',function ($table) {$table->increments('id');
$table->timestamps();
$table->temporary();
});
使用 Laravel 5.4、MySQL 5.6
当我连接到 laravel 之外的数据库时,我可以很好地从临时表中进行操作和选择
【问题讨论】:
标签: php mysql laravel temp-tables