Insert records from one table into another in Mysql?

Asked

Viewed 1,014 times

0

I’m having difficulty entering data from one table into another, for example I have the budget table that has the items, but the items may be from different suppliers, and when approved this budget the same should generate the purchase orders for the respective suppliers, example:

Budget table

Supposing SELECT * FROM orcamento WHERE orcamento = 330;, have:

inserir a descrição da imagem aqui

Purchase order table

I would like to create a purchase order for each supplier as in the table below, being dismembered as follows:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I would like to create a purchase order with all the items of the supplier as in the example of the purchase order table where an order was created with the items of Id 2 and 4 that correspond to the same supplier code 55, would like to use this feature directly in the bank informing only the budget id.

I’m using php with mysql, not mysqli.

1 answer

1


You can use a INSERT along with a SELECT.

For example:

INSERT INTO ordem_compra (id, id_orcamento, fornecedor)
SELECT id, id_orcamento, id_fornecedor FROM orcamento WHERE orcamento = 330;
  • then the problem is that this way I need to know the supplier id, I will inform the budget id that has several items and can make one item for each vendor or several from the same vendor, and the vendor id will only know after selecting from the budget.

  • I didn’t understand it very well. But you couldn’t change "vendor = 55" by budget id and then you’d have all the data from that budget in select.

  • I edited the question was really confused, I apologize.

  • @Wmomesso I edited the answer. But I suggest changing the classifications, it seems to me that the id in the ordem_purchase table is the item id and the id_oc that is the id of the purchase order.

  • @Diegoschidt all right, I made a Join Inner and it worked perfectly.

Browser other questions tagged

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