What you need to do is what is known as the Cartesian product or CROSS JOIN
, let's see a basic example:
create table empresas(
id int,
nombre varchar(255)
);
create table categorias(
id int,
nombre varchar(255)
);
insert into empresas(id, nombre) values (1, 'empresa1');
insert into empresas(id, nombre) values (2, 'empresa2');
insert into categorias(id, nombre) values (1, 'categoria1');
insert into categorias(id, nombre) values (2, 'categoria2');
With a CROSS JOIN
we managed to combine each record of one table with each record of the other:
select *
from empresas e
cross join categorias c
order by e.id;
The final result would be 2 x 2 = 4 records, something like this:
id nombre id nombre
--- ----------- --- -----------
1 empresa1 1 categoria1
1 empresa1 2 categoria2
2 empresa2 1 categoria1
2 empresa2 2 categoria2
If you are going to insert this into a ternary table, you should simply list only the ids
of each table.