How to insert by selecting from two different tables?

Asked

Viewed 4,604 times

0

TABELA 1
- ID
- IMOVEL
- CODIGO
- IMAGEM_G  <----- nessa tabela este dado é vazio.
- IMAGEM_P

TABELA 2
- ID
- IMOVEL    <----- nessa tabela este dado é vazio.
- CODIGO    <----- nessa tabela este dado é vazio.
- IMAGEM_G
- IMAGEM_P

I need a TABELA 3 thus:

- ID        <----- idêntico nas tabelas
- IMOVEL    <----- ID TABELA 1
- CODIGO    <----- ID TABELA 1
- IMAGEM_G  <----- ID TABELA 2
- IMAGEM_P  <----- idêntico nas tabelas

Can you help me ride this INSERT in TABELA 3?

  • Is this coming from a form? Have you already created the 3rd table? It would just make a normal index with the data you want in table 3

  • I will select in the database itself and make Insert of these inputs in a third table inside the database.

  • Then just do the SELECT of the data you want and then the INSERT with the return of the data in TAB_3, like a process?

2 answers

4


Can make a INSERT SELECT with the data:

INSERT INTO `TABELA 3` (ID, IMOVEL, CODIGO, IMAGEM_G, IMAGEM_P)
SELECT t1.ID, t1.IMOVEL, t1,codigo, t2.IMAGEM_G, t1.IMAGEM_P FROM `TABELA 1` t1
INNER JOIN `TABELA 2` t2 ON t1.id = t2.id

whereas ID is identical in both tables and its data is consistent.

  • With 2 or 3 micro scrambled worked. Now I have another problem. I have to do this Insert with 70MIL records. How can I split this? Because otherwise it will end the database. The trend of these records is only to increase.

  • You’d better ask another question?

  • Marcos, there’s another question. But I believe that this process should be done only once and then update your application to use only the TABELA 3 in the future, excluding the previous tables.

  • In fact, I am gathering information from the two that for performance reasons do not fit in a single INSERT that is responsible for generating the tables. It’s something I’ll need to do once a day.

  • @Marcosvinicius I find it interesting to review this strategy of the tables. Why not use only one? Sometimes when trying to solve a problem we create another two.

  • We can chat about it?

  • @Marcosvinicius won’t be possible now, I’ll only get in on Tuesday night.

  • In short, I have to take this data in a webservice in two queries because in one query the spool of the webservice bursts and gives error. That’s why I need two tables. I could update the second query but it was more complex for me than using two tables.

  • @Marcosvinicius do you need to download this data every day? 70,000 new records daily is a lot. Get in touch with what makes the web service available and see a way to download only new data.

Show 4 more comments

3

Select with php to get ids from Tabela1 and, within a loop, run the query down below.

INSERT INTO Tabela3 (id, imovel, codigo, imagem_g, imagem_p)
    VALUES (
        (SELECT id FROM Tabela1 WHERE id = $id),
        (SELECT imovel FROM Tabela1 WHERE id = $id),
        (SELECT codigo FROM Tabela1 WHERE id = $id),
        (SELECT imagem_g FROM Tabela2 WHERE id = $id),
        (SELECT imagem_p FROM Tabela1 WHERE id = $id)
    )

I advise you to put one LIMIT in the amount of records that will be entered at a time to not overload the bank.


Another way would be, within the loop of ids of Tabela1 take the data via a single query and then insert it into the Tabela3.

SELECT t1.id, t1.imovel, t1.codigo, t2.imagem_g, t1.imagem_p
    FROM Tabela1 t1
    JOIN Tabela2 t2 ON t2.id = t1.id
        WHERE t1.id = $id

INSERT INTO Tabela3 (id, imovel, codigo, imagem_g, imagem_p)
    VALUES ($res->id, $res->imovel, $res->codigo, $res->imagem_g, $res->imagem_p)
  • This variable $id does not tell me anything because I will select the data directly inside phpMyAdmin. I do not understand why to use it!!!

  • @Marcosvinicius You want to run this query directly in the/phpMyAdmin database or through PHP?

  • Direct in phpMyAdmin because I already have hundreds of records ready and only need to merge tables 1 and 2 in 3.

  • @Marcosvinicius Without using any language to go through the records I don’t know how to do, because somehow you need some filter to get the values. I will remove the PHP tag from your question.

Browser other questions tagged

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