I wanted to UPDATE multiple tables at the same time so I used LEFT JOIN to create a single query for my UPDATE. I tried 2 approach to make the LEFT JOIN but both failed. I don't see where i made the mistake so I am hoping someone might be able to dissect the query properly and point out the error.
I already applied some formatting to the query so that i would look readable than it used to look:
First is:
"UPDATE "
.
"table1 AS t1 SET t1.Bid = :id "
.
"LEFT JOIN table2 AS t2 SET t2.id = :id ON t1.Bid = t2.id AND t1.status = t2.status "
.
"LEFT JOIN table3 AS t3 SET t3.Bid = :id ON t1.Bid = t3.Bid AND t1.status = t3.status "
.
"LEFT JOIN table4 AS t4 SET t4.id = :id ON t1.Bid = t4.id AND t1.status = t4.status "
.
"LEFT JOIN table5 AS t5 SET t5.Bid = :id ON t1.Bid = t5.Bid AND t1.status = t5.status "
.
"LEFT JOIN table6 AS t6 SET t6.id = :id ON t1.Bid = t6.id AND t1.status = t6.status "
.
"LEFT JOIN table7 AS t7 SET t7.Bid = :id ON t1.Bid = t7.Bid AND t1.status = t7.status "
.
"LEFT JOIN table8 AS t8 SET t8.id = :id ON t1.Bid = t8.id AND t1.status = t8.status "
.
"WHERE t1.Bid = :oldid AND t1.status = :status "
Second one is:
$stmt = $dbh - > prepare("UPDATE "
.
"table1 AS t1 "
.
"LEFT JOIN table2 AS t2 ON t1.Bid = t2.id AND t1.status = t2.status "
.
"LEFT JOIN table3 AS t3 ON t1.Bid = t3.Bid AND t1.status = t3.status "
.
"LEFT JOIN table4 AS t4 ON t1.Bid = t4.id AND t1.status = t4.status "
.
"LEFT JOIN table5 AS t5 ON t1.Bid = t5.Bid AND t1.status = t5.status "
.
"LEFT JOIN table6 AS t6 ON t1.Bid = t6.id AND t1.status = t6.status "
.
"LEFT JOIN table7 AS t7 ON t1.Bid = t7.Bid AND t1.status = t7.status "
.
"LEFT JOIN table8 AS t8 ON t1.Bid = t8.id AND t1.status = t8.status "
.
" SET t1.Bid = :id, "
.
" SET t2.id = :id, "
.
" SET t3.Bid = :id, "
.
" SET t4.id = :id, "
.
" SET t5.Bid = :id, "
.
" SET t6.id = :id, "
.
" SET t7.Bid = :id, "
.
" SET t8.id = :id "
.
"WHERE t1.Bid = :oldid AND t1.status = :status ");
UPDATE
I am using the first option and i get:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN table2 AS t2 SET t2.id = '315-512-613-12' at line 1'
Second one gets:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET t2.id = '315-512-613-123V', SET t3.Bid = '315-512-613-123V', SE' at line 1'
I had it working using this query.
"UPDATE
table1 AS t1 LEFT JOIN
table2 AS t2 ON t1.Bid = t2.id AND t1.status = t2.status LEFT JOIN
table3 AS t3 ON t1.Bid = t3.Bid AND t1.status = t3.status LEFT JOIN
table4 AS t4 ON t1.Bid = t4.id AND t1.status = t4.status LEFT JOIN
table5 AS t5 ON t1.Bid = t5.Bid AND t1.status = t5.status LEFT JOIN
table6 AS t6 ON t1.Bid = t6.id AND t1.status = t6.status LEFT JOIN
table7 AS t7 ON t1.Bid = t7.Bid AND t1.status = t7.status LEFT JOIN
table7 AS t8 ON t1.Bid = t8.id AND t1.status = t8.status
SET t1.Bid = :id,
t2.id = :id,
t3.Bid = :id,
t4.id = :id,
t5.Bid = :id,
t6.id = :id,
t7.Bid = :id,
t8.id = :id
WHERE t1.Bid = :oldid
AND t1.status = :status "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With