SQL insert more than one record in a table in an INSERT

0

I would like to make a query where I insert a record that is related to 2 more tables.

MaximosyMinimos

INSERT INTO MaximosYMinimos
(ProductoID, SucursalID, Max, Min) 
VALUES
((select p.ID from Productos as p where p.ID = 1325 ), (select s.ID from Sucursales as s where s.ID = 1), 0, 0)

but I want to enter all the records of the product table only the ID, of the same for Branches. Taking the N records of the 2 previous tables, and enter them to the table of maximums.

MaximosyMinimos

ID | IProductoID | SucursalID | Max | Min

1    102           20            0    0
2    103           20            0    0
3    105           20            0    0
4    106           20            .    .
5    102           21
6    103           21
7    104           21
8    105           21
9    102           22
10   103           22
11   104           22

Where the ID 20 is, for example, branch1 and 21 branch2 and so on. Thanks.

    
asked by JuanL 19.02.2018 в 21:23
source

1 answer

1

If what you want to do is the Cartesian product of the tables Productos and Sucursales , and insert them into a table together with the value 0 for the fields Max and Min , then the query is the following:

insert into MaximosYMinimos (ProductoID, SucursalID, Max, Min)
select p.ID, s.ID, 0, 0 from Productos as p, Sucursales as s
    
answered by 20.02.2018 / 14:23
source