mysql - Inserting data from a join within tables -


i'm trying figure out how extend table of company_ids using group reference table. given company_name, want company_ids, join them company_groups table, , group_ids. there, want every company_id associated group_id, , insert them in companies table proper company_name.

the structure bit atypical. single company name can have many ids, because id isn't used unique identifier single company. i'm trying make sure of them copied companies table. i'm sure there's simple join command, i've never been @ joining tables within themselves. can help? thanks!

+---------------+ |   companies   | +---------------+ | company_id pk | | company_name  | +---------------+  +----------------+ | company_groups | +----------------+ | company_id pk  | | group_id       | +----------------+ 

here's example. in companies table, have company "tesla", 3 different ids.

+--------------+------------+ | company_name | company_id | +--------------+------------+ | tesla        |     647552 | | tesla        |     927572 | | tesla        |     748563 | +--------------+------------+ 

in company_groups table, each group_id has 1 or more company_ids.

+----------+------------+ | group_id | company_id | +----------+------------+ | 227      |     647552 | | 227      |     111743 | | 227      |     111842 | +----------+------------+ 

i want match company_name multiple group_ids, find company_ids associated group. new company_ids inserted in companies table this:

+--------------+------------+ | company_name | company_id | +--------------+------------+ | tesla        |     647552 | | tesla        |     927572 | | tesla        |     748563 | | tesla        |     111743 | | tesla        |     111842 | +--------------+------------+ 

you cannot insert table when same table name appear in clause.

you can use temporary third table job

at second thought, question either funny or not clear enough, please provide sample data, can update answer


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -