MySql 一次更新两个表

发布于 2024-08-25 06:19:18 字数 410 浏览 5 评论 0原文

我有两个表需要完全相同的值来实现非规范化目的。

这是查询。

第一个表

UPDATE Table_One 
SET win = win+1, streak = streak+1, score = score+200 
WHERE userid = 1 AND lid = 1 LIMIT 1

第二个表

UPDATE Table_Two 
SET win = win+1, streak = streak+1, score = score+200 
WHERE userid = 1 LIMIT 1

正如您所看到的,两个表之间的唯一区别是它们的名称,并且第二个表没有字段 lid

无论如何,要将两个更新合并为一个?

I have two tables that need the exact same values for denormalization purposes.

Here's the query.

first table

UPDATE Table_One 
SET win = win+1, streak = streak+1, score = score+200 
WHERE userid = 1 AND lid = 1 LIMIT 1

second table

UPDATE Table_Two 
SET win = win+1, streak = streak+1, score = score+200 
WHERE userid = 1 LIMIT 1

As you can see the only difference between both tables is their name and table two doesn't have the field lid

Anyway to combine both updates to just one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断肠人 2024-09-01 06:19:32

它们是两个独立的查询,因此必须如此对待。很抱歉这么说,但是如果您要更新两个具有相同数据的表,可能有更好的方法来设计数据库。请记住保持您的节目干燥

编辑:应该撤回该内容;您可以将其用于多个表,但不能使用 ORDER BYLIMIT

They’re two separate queries and so must be treated as such. Sorry to say it, but if you’re updating two tables with identical data, there’s probably a better way to design your database. Remember to keep your programming DRY.

Edit: Should retract that; you can use it for multiple tables, but you can’t use ORDER BY or LIMIT.

蔚蓝源自深海 2024-09-01 06:19:26

如果您可以连接这些表,那么您可以创建两个表的视图,然后通过该视图进行更新。在您的示例中,userid 看起来可能是合适的键。

在创建视图时,您需要遵循以下内容:指南。

If you can join the tables, then you could create a view of two tables, then update via that view. In your example it looks like userid might be a suitable key.

In creating the view, you'd need to stick to the following guidelines.

倾城花音 2024-09-01 06:19:25

如果从 Table_One 到 Table_Two 存在一对一或一对多关系,则可以使用以下方法:

UPDATE Table_One T1, Table_Two T2 
SET T1.win = T1.win+1, T1.streak = T1.streak+1, T1.score = T1.score+200, 
    T2.win = T2.win+1, T2.streak = T2.streak+1, T2.score = T2.score+200 
WHERE T1.userid = 1 AND T1.lid = 1 AND T2.userid = T1.userid;

If there is a one to one or one to many relation from Table_One to Table_Two, this would work:

UPDATE Table_One T1, Table_Two T2 
SET T1.win = T1.win+1, T1.streak = T1.streak+1, T1.score = T1.score+200, 
    T2.win = T2.win+1, T2.streak = T2.streak+1, T2.score = T2.score+200 
WHERE T1.userid = 1 AND T1.lid = 1 AND T2.userid = T1.userid;
凉墨 2024-09-01 06:19:23

如文档中所述,应该可以进行多表更新。

http://dev.mysql.com/doc/refman/5.5/en/update.html< /a>

UPDATE Table_One a INNER JOIN Table_Two b ON (a.userid = b.userid)
SET
  a.win = a.win+1, a.streak = a.streak+1, a.score = a.score+200,
  b.win = b.win+1, b.streak = b.streak+1, b.score = b.score+200 
WHERE a.userid = 1 AND a.lid = 1 AND b.userid = 1

注意:多表不支持 LIMIT,因此这可能会导致更多的麻烦,具体取决于细节。

存储过程或事务可能是更好的解决方案。

It should be possible with a multi-table update, as described in the documentation.

http://dev.mysql.com/doc/refman/5.5/en/update.html

UPDATE Table_One a INNER JOIN Table_Two b ON (a.userid = b.userid)
SET
  a.win = a.win+1, a.streak = a.streak+1, a.score = a.score+200,
  b.win = b.win+1, b.streak = b.streak+1, b.score = b.score+200 
WHERE a.userid = 1 AND a.lid = 1 AND b.userid = 1

Note: Multi-table doesn't support LIMIT, so this could cause more grief depending on the details.

Stored procedures or transactions may be a nicer solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文