简体   繁体   中英

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...

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; 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

Make sure your select returns only one record a an according where clause. Then use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM