INSERT 0..n records into table 'A' based on content of table 'B' in MySql 5

Posted by Robert Gowland on Stack Overflow See other posts from Stack Overflow or by Robert Gowland
Published on 2010-06-02T17:12:59Z Indexed on 2010/06/02 19:54 UTC
Read the original article Hit count: 178

Filed under:
|

Using MySql 5, I have a task where I need to update one table based on the contents of another table.

For example, I need to add 'A1' to table 'A' if table 'B' contains 'B1'. I need to add 'A2a' and 'A2b' to table 'A' if table 'B' contains 'B2', etc.. In our case, the value in table 'B' we're interested is an enum.

Right now I have a stored procedure containing a series of statements like:

INSERT INTO A
SELECT 'A1'
FROM B
WHERE B.Value = 'B1';
--Repeat for 'B2' -> 'A2a'; 'B2' -> 'A2b'; 'B3' -> 'A3', etc...

Is there a nicer more DRY way of accomplishing this?

Edit: There may be values in table 'B' that have no equivalent value for table 'A'.

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql