Update records from one table using values from another table

Asked

Viewed 33,562 times

2

I need to update a table with the value of another table and there are many records so why I’m doing it this way, but my query is not working:

UPDATE produtos SET produtos.fornecedor = movimentacao.Fornecedor FROM produtos,
movimentacao WHERE produtos.codigo = movimentacao.Codigo ORDER BY movimentacao.Data DESC LIMIT 1

ERROR : #1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'FROM products, move where products.code = movement. ORDER B' code at line 3

Does anyone know how to resolve this syntax error?

  • Gave a general answer for what you asked. If you give more details about the need for limit and order, I can try to think of something better.

  • @Bacco O ORDER BY is that I want to update with the value of the latest record and the LIMIT to limit to the latest value

  • I think it will be the case of subquery then. I will make a test and I give a feedback.

3 answers

6

You need a UPDATE JOIN in this case:

UPDATE produtos 
   [INNER JOIN | LEFT JOIN] movimentacao ON produtos.codigo = movimentacao.Codigo
   SET produtos.fornecedor = movimentacao.Fornecedor
   WHERE condicao

The problem in this case is that you can’t use ORDER BY. You’d have to see if a Where would solve your specific case.

To maintain the ORDER BY, the solution would be a subquery:

UPDATE produtos
   SET produtos.fornecedor = (
      SELECT movimentacao.Fornecedor
         FROM movimentacao
         WHERE produtos.codigo = movimentacao.Codigo
   )
   ORDER BY movimentacao.Data DESC
   LIMIT 1
  • Only had a small problem, ORDER BY was out of SELECT but other than that all right.

  • 3

    Order By out is to update the latest. Inside, as in the approved answer, it does not make much sense, unless it has repetitions, which is not clear in the question. But given the "history", I understand the Accept perfectly. The good thing is that you have solved the problem.

  • Yes, it is that I want to update them but with the most recent searched data, but the problem has been solved..

6


I believe whatever you want is this

UPDATE Campeonato 
SET Nome =(SELECT Nome FROM Time WHERE Campeonato_Id= Campeonato.Id AND Position = '1')
WHERE
Ano ='2013'

In the above example I am updating the Nome of all the Campeonatos of Year of 2013 to the Nome of Time corresponding to Championship and which occupies the first position.

I don’t understand very well the query you want to do but following this same example I believe you can succeed.

UPDATE produtos 
SET produtos.fornecedor = (SELECT movimentacao.Fornecedor 
 FROM movimentacao WHERE movimentacao.Codigo = produtos.codigo
 ORDER BY movimentacao.Data DESC LIMIT 1
)
WHERE
 condicao

0

I have a question, I need to subtract a row (quantity) from the table (sales), which was inserted by the user in the form, in the table (products) in the line (stock), and only the last record registered in the table (products) has to be made. I made a sales registration form, where it inserts the data in the table for registration, and then created an update to update the value of the stock (stock-quantity=stock). My php code I’m using is this:

     <?php 
include 'conexao.php';
$id = $_POST['id'] // Este id só existe na tabela de vendas, na tabela de produto é id_produtos
$cd_barras = $_POST["cd_barras"] // 
$nomeproduto =$_POST["nomeproduto"]
$preco_venda = $_POST["preco_venda"]
$quantidade = $_POST["quantidade"]
$tamanho = $_POST["tamanho"]

$decqtde = $quantidade ; // variável para subtração estoque-quantidade

$sql = "INSERT into vendaprodutos (cd_barras, nomeproduto, preco_venda, quantidade, tamanho) VALUES (NULL,'$cd_barras','$nomeproduto','$preco_venda','$quantidade','$tamanho')";
mysql_query($sql,);

$sql = "UPDATE produtos set produtos.estoque = vendaproduto.quantidade WHERE id = '$cd_barras'; "; 
mysql_query($sql); 

header("Location: venda.php");
?>

They can help me because I searched some forums and could not solve.

  • Welcome, Thiago. Use the button Ask a question that is at the top/right of the screen to ask a question. The answer field should be used only to answer the question. ;)

  • Thank you, I asked the question: http://answall.com/questions/73395/subtrair-linha-de-uma-tabela-por-other-table

Browser other questions tagged

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