Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

Here is an example:

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

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

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)

How can I do something like this:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
147 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

For example

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...