我正在xampp中使用Laravel 5.4和PHP 7.2。我试图截断一个临时表,然后从MySQL视图插入新数据。该代码将执行并完全执行我想要的操作,但是它表示存在错误-
SQLSTATE [HY000]:一般错误(SQL:插入到prod_joins_temp中 (日期,代码,sid,time_start,time_end,制成,第一,秒,p_broken,q_broken,g_broken,prod_id) 从prod_joins中选择日期,代码,sid,time_start,time_end,制成,第一,秒,p_broken,q_broken,g_broken,prod_id)
最初在视图中插入两列:General error: 1615 Prepared statement needs to be re-prepared
和time_start
列后,出现以下错误time_end
。
我尝试删除两列以查看是否是问题所在,但现在我都陷入了可怕的一般错误。
我尝试了这个答案Laravel: General error: 1615 Prepared statement needs to be re-prepared。它没有用,并且根据评论将导致问题ak验证。
我已经找到了答案PDO error: “ SQLSTATE[HY000]: General error ” When updating database,这与我的问题完全相同,但是它没有回答我的问题,因为我没有使用fetchAll()
,也没有使用任何变量的重复变量完全没有。
我尝试了以下两种实现目标的方法。
截断表prod_joins_temp
ProdJoinsTemp::truncate();
然后从视图prod_joins填充表prod_joins_temp
DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins');
给我完全相同错误的另一种方法如下
删除表prod_joins_temp(如果存在)
\Schema::dropIfExists('prod_joins_temp');
然后从视图prod_joins创建表prod_joins_temp
DB::select('CREATE TABLE prod_joins_temp AS SELECT * FROM prod_joins;');
这里是构成视图的代码
SELECT
`scorecard54`.`production`.`date` AS `date`,
`scorecard54`.`products`.`code` AS `code`,
`scorecard54`.`production`.`sid_production` AS `sid`,
`scorecard54`.`production`.`time_start` AS `time_start`,
`scorecard54`.`production`.`time_end` AS `time_end`,
`scorecard54`.`production`.`made` AS `made`,
`scorecard54`.`qcontrol`.`firsts` AS `firsts`,
`scorecard54`.`qcontrol`.`seconds` AS `seconds`,
`scorecard54`.`production`.`broken_production` AS `p_broken`,
`scorecard54`.`qcontrol`.`broken_qcontrol` AS `q_broken`,
`scorecard54`.`grinding`.`discarded` AS `g_broken`,
`scorecard54`.`products`.`prod_id` AS `prod_id`
FROM
(
(
(
`scorecard54`.`production`
LEFT JOIN `scorecard54`.`products` ON
(
(
`scorecard54`.`production`.`prod_code` = `scorecard54`.`products`.`prod_id`
)
)
)
LEFT JOIN `scorecard54`.`grinding` ON
(
(
`scorecard54`.`production`.`sid_production` = `scorecard54`.`grinding`.`sid_grinding`
)
)
)
LEFT JOIN `scorecard54`.`qcontrol` ON
(
(
`scorecard54`.`grinding`.`tray_id_grinding` = `scorecard54`.`qcontrol`.`tray_id_qcontrol`
)
)
)
WHERE
(
`scorecard54`.`grinding`.`have_qcontrol` = 1
)
这是从视图拖放并创建表的函数
public function generateTable(Request $request) {
// Drop temp table
\Schema::dropIfExists('prod_joins_temp');
// re create table with the new data
DB::select('CREATE TABLE prod_joins_temp AS SELECT * FROM prod_joins;');
$request->session()->flash('alert-success', 'Success: Reports table regenerated.');
return Redirect::to('/statistics/');
}
我也尝试过截断然后使用此功能重新填充
public function generateTable(Request $request) {
// Drop temp table
ProdJoinsTemp::truncate();
// re generate table with the new data
DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins');
$request->session()->flash('alert-success', 'Success: Reports table regenerated.');
return Redirect::to('/statistics/');
}
我在这里做错了什么?是否存在某种视图缓存?
答案 0 :(得分:0)
我已通过此答案SQLSTATE[HY000]: General error: 2053 error occurs at Laravel
解决了此问题在使用以下命令更新表之前
DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins');
我将查询的选择部分更改为要更新,并且一切正常。请参阅以下
DB::update('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins');
在将字段添加到视图中之前,我不知道为什么以前的方法可行,但是现在它要求我使用update而不是select