Popular table using two that has no link

Asked

Viewed 88 times

-1

Guys I’m having a little problem I have the following situation:

  • table escritorio where I have a field called id that I’m gonna need
  • table etapas where I have the fields id that I’m gonna need;

I need to popular a table called etapasemuso where it has the following structure:

etapasemuso
emusoid int,
etapasid int,
escritorioid int,
ativo char(1),
etapaok char(1)

To popular this table above I need to search for data in the table escritorio and stages, but I can not connect the same since there is no key between them. Would anyone know how to set up a query for popular that table above?

  • But if there is no link between the two tables how do you intend to combine the data of the two? Want to make a simple Cartesian product, this is each office table entry combined with all existing entries in the table steps? This makes sense?

  • Exact all office entries combined with steps

  • There doesn’t need to be a key to be making a Join between tables, now if they don’t a common field is going to be impossible, it’s like you want to link a table of "people" with a table of "minerals on Mars", it’s not possible to relate, and it wouldn’t make sense. In this case, it will be a Cartesian as @anonimo mentioned, then just put the two tables in the from and be happy (or not) ;)

  • something like select escritorio.id, etapas.id from escritorio, etapas

  • Use a CROSS JOIN, it would be something like: INSERT INTO etapasemuso(etapasid, office) SELECT steps.id, office.id FROM steps, office; .

  • Both Cross Join with select above solved my problem thanks for helping one more to collect that cross Join hugs everyone

Show 1 more comment

1 answer

0

You can make a CROSS JOIN that is nothing more than a JOIN no links between the tables, which will "join" all the records in table 1 with all the records in table 2:

INSERT INTO etapasemuso(etapasid, escritorioid)
SELECT e1.id, e2.id
  FROM etapas e1
 CROSS JOIN escritorio e2
  • Perfect helped me a lot I didn’t know the cross Jay worked

  • @Ederluca do not forget to accept the answer if it has helped you

Browser other questions tagged

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