Update MYSQL Table from multiple rows in another table

user6630782

I'm trying to update table yy from table xx results by doing sum.

For example (syntax is abstract):

update table_yy
  set sum_of_x_and_y = (
       (select sum(row_x) from table_xx where class_id=1)
                 +
       (select sum(row_y) from table_xx where class_id=1) )

Table xx

row_id   class_id   row_x   row_y
   1        1        4        5
   2        1        5        6
   3        2        6        7
   4        1        7        8

Table yy

class_id   sum_of_x_and_y
   1            35
   2            13

but instead of setting the class_id manually, I would love to do something like inner-join update, but I'm working with 15k+ of records.

David Cros Piezzoli

Here is a query that should do the job

UPDATE table_yy, (
    SELECT class_id, SUM(table_xx.row_x + table_xx.row_y) AS sum_of_x_and_y
    FROM table_xx
    GROUP BY table_xx.class_id 
) AS table_sum
SET table_yy.sum_of_x_and_y = table_sum.sum_of_x_and_y
WHERE table_yy.class_id = table_sum.class_id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MYSQL - UPDATE multiple rows from another table

From Dev

MySQL: Update rows in table, from rows with matching key in another table

From Dev

insert multiple rows mysql from another table

From Dev

Update multiple rows in one table with differing values from another

From Dev

Mysql - update column from another table data without reducing rows

From Dev

MySQL select and update multiple rows from same table

From Dev

MySQL - Update table values from another table

From Dev

MySQL Table won't update multiple rows

From Dev

How to update multiple rows in a table to MySQL

From Dev

Update specific rows from one table to another

From Dev

Insert multiple rows from select into another table

From Dev

Field involving multiple rows from another table

From Dev

Update table from another table throught intermediate table Mysql

From Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

From Dev

MySQL - Select multiple rows from one table whose IDs are stored in another table

From Dev

looping mysql query to get multiple rows of data from a table and insert into another table

From Dev

mysql - move rows from one table to another

From Dev

mysql: select rows from another table as columns

From Dev

MySQL - Join & Count rows from another table

From Dev

Link one row of table to multiple rows in another table in MySQL

From Dev

MySQL query - single row in one table with multiple rows in another table

From Dev

How to insert multiple records from one table into another on update of third table in MySQL

From Dev

MySQL update with select from another table

From Dev

Trigger to update sum from another table MySQL

From Dev

mysql update an column from another table

From Dev

MySQL update with select from another table

From Dev

Trigger to update sum from another table MySQL

From Dev

mysql update column value from another table

From Dev

MySQL update column from another table

Related Related

  1. 1

    MYSQL - UPDATE multiple rows from another table

  2. 2

    MySQL: Update rows in table, from rows with matching key in another table

  3. 3

    insert multiple rows mysql from another table

  4. 4

    Update multiple rows in one table with differing values from another

  5. 5

    Mysql - update column from another table data without reducing rows

  6. 6

    MySQL select and update multiple rows from same table

  7. 7

    MySQL - Update table values from another table

  8. 8

    MySQL Table won't update multiple rows

  9. 9

    How to update multiple rows in a table to MySQL

  10. 10

    Update specific rows from one table to another

  11. 11

    Insert multiple rows from select into another table

  12. 12

    Field involving multiple rows from another table

  13. 13

    Update table from another table throught intermediate table Mysql

  14. 14

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  15. 15

    MySQL - Select multiple rows from one table whose IDs are stored in another table

  16. 16

    looping mysql query to get multiple rows of data from a table and insert into another table

  17. 17

    mysql - move rows from one table to another

  18. 18

    mysql: select rows from another table as columns

  19. 19

    MySQL - Join & Count rows from another table

  20. 20

    Link one row of table to multiple rows in another table in MySQL

  21. 21

    MySQL query - single row in one table with multiple rows in another table

  22. 22

    How to insert multiple records from one table into another on update of third table in MySQL

  23. 23

    MySQL update with select from another table

  24. 24

    Trigger to update sum from another table MySQL

  25. 25

    mysql update an column from another table

  26. 26

    MySQL update with select from another table

  27. 27

    Trigger to update sum from another table MySQL

  28. 28

    mysql update column value from another table

  29. 29

    MySQL update column from another table

HotTag

Archive