gpt4 book ai didi

mysql - 如何在 UPDATE mysql 查询中使用 JOIN

转载 作者:行者123 更新时间:2023-11-29 11:52:53 24 4
gpt4 key购买 nike

您好,我想通过 JOIN 将其绑定(bind)到一个查询。这是如何工作的:

Db::bind("uid", strip_tags($userid));
DB::bind("user_id", strip_tags($refer));
Db::bind("points_earn", strip_tags($points_earn));
Db::bind("points_refer", strip_tags($points_refer));
Db::query("UPDATE referrals SET `points_earn` = :points_earn WHERE new_id = :uid");
Db::query("UPDATE users SET `points` = `points` + :points_refer WHERE id = :user_id");

我的想法但行不通。

Db::query("UPDATE referrals r JOIN users u ON r.user_id = u.id SET `r.points_earn` = :points_earn WHERE r.new_id = :uid AND `u.points` = `u.points` + :points_refer WHERE u.id = :user_id");

有人有解决办法吗?

最佳答案

你的语法是正确的,但是后面的勾号是错误的,并且每个SELECT只有一个WHERE:

UPDATE referrals r JOIN
users u
ON r.user_id = u.id
SET r.points_earn = :points_earn
WHERE r.new_id = :uid AND u.points = u.points + :points_refer AND
u.id = :user_id;

反引号的问题在于以下内容有很大不同:

`r.points_earn`
`r`.`points_earn`

第一个引用名为“r.points_earn”的列。第二个引用“r”中的“points_earn”列。反引号不是必需的,因此您可以将其删除。

关于mysql - 如何在 UPDATE mysql 查询中使用 JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33708590/

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