Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

php - Updating multiple rows with an array

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...