MySql: Insert data from one table to another

Viewed 202
Table A:
+----+-------+--------+-------+
| id | Tag   | Name   | Size  |
+----+-------+--------+-------+
|  1 | One   | Bob    | 14.29 |
|  2 | Two   | Charles| 28.56 |
|  3 | Three | Frank  | 76.95 |
+----+-------+--------+-------+
Table B:
+----+-------+--------+-------+
| id | Tag   | Name   | Size  |
+----+-------+--------+-------+
|  55| Two   | Charles|     0 |
|  68| Three | Frank  |     0 |
+----+-------+--------+-------+

What I want: Size from Table A in Table B

Closest query:

  INSERT INTO B (Size) 
  SELECT Size FROM A 
  WHERE A.Tag = B.Tag 
  AND A.Name = B.Name;

Resulting in error: ERROR 1054 (42S22): Unknown column 'B.Tag' in 'where clause'.

I am newbie to MySql and guess this is a standard database task.


1 Answers

What you need is to update the table B and not insert new rows.
Use a join in the UPDATE statement:

UPDATE B
INNER JOIN A ON A.Tag = B.Tag AND A.Name = B.Name
SET B.Size = A.Size;
Related