add values from one table and put the result in another

Asked

Viewed 793 times

2

I have a table called players and I need to add the values of the column points but only those that have the same account_id and after placing the result in another table called Accounts in which have the indicated id. The result would be this:

Table players:

account_id    |pontos      
1             |50  
1             |100               
2             |25              
2             |20     

Table Accounts:

id            |pontos      
1             |150
2             |45
  • 1

    You need a INSERT INTO SELECT. I will now owe the SQL, but is the tip for who can answer you with more details.

1 answer

2


try this:

INSERT INTO accounts values (select account_id as id, SUM(pontos) from players group by account_id);
  • I tried but kept giving the following error: #1064 - You have an error in your SQL syntax; .

  • I managed as follows: UPDATE Accounts, players SET Accounts.points = ( Accounts.points + ( SELECT SUM(points) FROM players WHERE.players.points > 0 AND players.account_id = Accounts.id GROUP BY account_id ) ) WHERE players.points > 0 AND players.account_id = Accounts.id; Anyway it helped a lot in the development. Thank you.

  • Ah, the syntax error I believe was the word values before select, just remove it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.