Merge two tables into a third table

0

I need to merge two tables imagem1 and imagem2 into a third table imagens with SQL . They have exactly the same structure but with 1 query I can not write all the data at once, so I need to do 2 queries.

I need a SQL to merge the tables into a third call imagens . The starting point is the one shown in the picture, I can not do anything different before that point.

The two tables have this structure ....

  • ID
  • IMOVEL
  • CODE
  • IMAGE_G
  • PICTURE_p

In imagem1 you do not have the IMAGE_G data while in imagem2 you do not have IMOVEL and CODE .

In the third call imagens I need all this information together.

    
asked by anonymous 18.01.2015 / 14:10

2 answers

3

Considering that the correspondence between the tables is by the ID column, I did not understand why I would need two queries. I would do so:

INSERT INTO imagens
(id, imovel, codigo, imagem_g, imagem_p)
SELECT 
    imagem1.id,
    imagem1.imovel,
    imagem1.codigo,
    imagem2.imagem_g,
    NULL -- Se tiver imagem_p em imagem2, coloque aqui
FROM imagem1
    INNER JOIN imagem2
    ON imagem2.id = imagem1.id
    
18.01.2015 / 14:16
0

You can just update one of the tables as the example below, of course after updating your structure.

UPDATE docs 
 INNER JOIN docs_1 
   ON docs_1.id=docs.id
SET docs.teste2=docs_1.teste;
    
27.04.2018 / 21:23