INSERT and INNER JOIN together in Mysql?

Asked

Viewed 6,413 times

0

Hello is possible to use the INNER JOIN together with the INSERT?

I have a table A with 3 columns X, Y and Z and table B with columns W, X and Y and I need to insert in table A the column W of table B, how could it be? I’m trying this way but I don’t get results :

INSERT INTO Categoria_cliente_teste  (planejamento) 
SELECT plano_orcamentario 
FROM Categoria_clientes 
INNER JOIN [ON cliente_fornecedor]
  • It is possible and quite common to use INSERT ... SELECT (depending on the case and you don’t even need JOIN). What did you try and what problem was found? EX: INSERT INTO tabela A .... campo1, campo2 SELECT B.algo, C.algo FROM tabelaB JOIN tabelaC ON ...

  • Yes, there’s something to do with Inner Join, but it would be interesting to exemplify what you want to do. Add a better description of tables and code you’ve tried.

  • 1

    put what you tried into the question, to make it easier for those who see after

1 answer

2

Siim is possible!!

According to your description, I created the following example:

1- I created a database named "test"

2- I created the following tables:

Table: to

Columns: x, y and z

-

Table: b

Columns: w, x, y

An example of Insert would be: INSERT INTO test.a ('y', 'z') SELECT b.x, b.y FROM test.b b;

You can use an Inner Join to select the results you want to enter normally, I advise you to give an alias to the table of the query, as it is easier to associate to the values.

With the JOIN: INSERT INTO test.a ('y', 'z') SELECT b.x, b.y FROM test.b b JOIN c ON c.y = b.y;

:)

Browser other questions tagged

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