gpt4 book ai didi

php - 使用 LEFT JOIN pdo php mysql 更新

转载 作者:可可西里 更新时间:2023-11-01 08:40:28 24 4
gpt4 key购买 nike

我想同时更新多个表,所以我使用 LEFT JOIN 为我的更新创建一个查询。我尝试了 2 种方法来创建 LEFT JOIN,但都失败了。我看不出我在哪里犯了错误,所以我希望有人能够正确地剖析查询并指出错误。

我已经对查询应用了一些格式,这样我看起来比以前看起来更易读:

首先是:

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

更新

我正在使用第一个选项,我得到:

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'

最佳答案

我使用这个查询让它工作。

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

关于php - 使用 LEFT JOIN pdo php mysql 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33956627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com