簡體   English   中英

嘗試使用子查詢將數據插入字段

[英]Trying to insert data into a field using a subquery

我想使用帶有timediff函數的子查詢來填充數據庫中的字段...

在我看來,這是一個語法錯誤。 所以這是我的代碼:

$sql = "INSERT INTO paros (tipo, descripcion, ho, hf, totaltiempo(select 
timediff(hf, ho) from paros)) values (?,?,?,?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($tipo,$descripcion, $startTime, $endTime, 
$totaltiempo));
Database::disconnect();

我收到此錯誤:

致命錯誤:未被捕獲的PDOException:SQLSTATE [42000]:語法錯誤或訪問沖突:1064您的SQL語法有錯誤;請參閱附錄A。 檢查與您的MariaDB服務器版本相對應的手冊以獲取正確的語法,以在'(從paros中選擇timediff(hf,ho)))值附近使用(C:\\的第1行的'Paro no programado','Ajuste de pa' xampp \\ htdocs \\ oeemoldeo \\ paros.php:43堆棧跟蹤:#0 C:\\ xampp \\ htdocs \\ oeemoldeo \\ paros.php(43):PDOStatement-> execute(Array)#1 {main}拋出在第43行

確保您的select僅返回一條記錄where子句的記錄。 然后使用:

INSERT INTO paros (tipo, descripcion, ho, hf, totaltiempo) 
select ?, ?, ?, ?, timediff(hf, ho) from paros where ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM