使用数组更新多行 [英] Updating multiple rows with an array

查看:18
本文介绍了使用数组更新多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存用户信息的表.其中一列保存用户在他们所在游戏中的位置.
在创建游戏时,我需要更新每个团队的用户位置.

I have a table that holds user information. One of the columns holds the position of the user in the game they are in.
When a game is being created, I need to update the positions of the users of each team.

这是一个例子:

Game id : 7
Team 1 users : 1,2
Team 2 users : 3,4

team1_position : array(1,2)
team2_position : array(13,14)

我想要做的是使用 SET 区域中的位置数组更新用户表.
我的目标是能够在不需要用户 ID 的情况下更新用户(我有不同尺寸的游戏板,所以我有每个板尺寸的多个位置数组)

What I want to do is update the user table using the array of positions in the SET area.
My goal is to be able to update the users without the need for their id (I have different size game boards, so I have multiple position arrays for each board size)

我该怎么做:

UPDATE user 
SET position='(team1_position)' 
WHERE game = '7' AND team = '1'

我觉得选择每个团队的所有id并单独更新它们会浪费资源.

I feel like it would be a waste of resources to select all the id's of each team and update them separately.

推荐答案

我很难理解你想要做什么,如果能更好地解释一下就好了.据我了解,您正在从表中选择数据以更新其他表.您是否尝试过使用UPDATE .. JOIN .."查询?这应该允许您根据另一个表中的关联数据更新一个表中的多行.

I have a hard time understanding what you are trying to do, better explanation would be nice. From what I understand you are selecting data from tables in order to update other tables. Have you tried using an "UPDATE .. JOIN .." query? This should allow you to update multiple rows from one table based on associative data from another table.

例如

UPDATE user
JOIN game ON
    user.id=game.id_user
SET user.position=game.team1_position

显然这不适用于您的代码,因为我没有什么信息可以继续,但这应该让您知道要使用什么.

Obviously this wont work with your code as I have very little info to go on, but that should give you an idea of what to go with.

抱歉,如果我完全无法理解您的问题,根据您提供的信息,很难理解您的确切问题.

Sorry if I'm totally off in understanding your problem, as said it's a bit hard to understand your exact issue based on what you've given us.

这篇关于使用数组更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆