简体   繁体   English

尝试使用子查询将数据插入字段

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

I want to populate a field from my DB, using a subquery with timediff function... 我想使用带有timediff函数的子查询来填充数据库中的字段...

it seems to me like it is a syntax error. 在我看来,这是一个语法错误。 so this is my code: 所以这是我的代码:

$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();

And I am getting this error: 我收到此错误:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 致命错误:未被捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参阅附录A。 check the manual that corresponds to your MariaDB server version for the right syntax to use near '(select timediff(hf, ho) from paros)) values ('Paro no programado','Ajuste de pa' at line 1 in C:\\xampp\\htdocs\\oeemoldeo\\paros.php:43 Stack trace: #0 C:\\xampp\\htdocs\\oeemoldeo\\paros.php(43): PDOStatement->execute(Array) #1 {main} thrown on line 43 检查与您的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行

Make sure your select returns only one record a an according where clause. 确保您的select仅返回一条记录where子句的记录。 Then use: 然后使用:

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