我想同时更新多个表,所以我使用LEFT JOIN为我的UPDATE创建单个查询。我尝试了2种方法使左连接,但都失败了。我没有看到我犯了什么错误,所以我希望有人能够正确剖析查询并指出错误。用LEFT JOIN更新pdo php mysql
我已经应用某些格式的查询,使我看起来可读比以前看:
首先是:
"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 "
第二个是:
$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
我使用的第一个选项,我得到:
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'
第二个获得:
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'
那么,有什么问题呢?请包括这两个请求的预期和实际结果。我相信**更新**多个表的Mysql语法是'UPDATE t1,t2,t3 SET t1.field1 = value1,t2.field2 = value2 ... WHERE t1.pk = t2.fk ...' –
Check执行查询时由MySQL返回的错误。 – Jocelyn
@AlexBlex我期望它更新,但我得到错误..我会检查错误,并将其粘贴在更新..我不需要使用左连接是你的意思?但如果具体的ID不存在这意味着它不会被更新,这就是为什么我使用'LEFT JOIN'的原因之一 – guradio