I select data from tab1
to a variable, and use it in another query, which updates tab1
. Target column target
is of type INT, so I round it before its set.
It works well in mysql workbench, also getting no error from php (error_reporting set to -1). It just doesn't really update the target column - it stays zero.
Any idea why?
$q = 'SELECT @tmp = ( SELECT t
FROM tab1
WHERE a_id = :id1 );
UPDATE tab1
SET target = ( SELECT ROUND( @tmp / ( SELECT COUNT(*)
FROM tab2
WHERE b_id = :id2 )));
';
$sth = $dbh->prepare( $q );
$sth->execute( array( ':id1' => $id, ':id2' => $id ) );