Pergunta

I have trouble using the output of one query as a value in another. The thing is I need to get the price of one product (which is successfully accomplished) using the following query:

SELECT Cena FROM Proizvod WHERE ProzivodID = $PID

$PID is a PHP variable and this query works just fine. The problem comes when I want to subtract the price of the product from the buyer's balance. I tried to do that with the following query:

UPDATE Kupuvac SET Saldo = Saldo - ".$cena." WHERE KupuvacID=$KID

$cena is a PHP variable, the value of which is the first query.

The second query doesn't work at all, it doesn't return an error message, but it doesn't change he buyer's balance.


A short dictionary if someone needs it: Cena - Price; Proizvod - Product; Kupuvac - Buyer; Saldo - Balance.

Thanks in advance!

Foi útil?

Solução

You can do your UPDATE operation in one statement:

UPDATE Kupuvac a
CROSS JOIN
(
    SELECT Cena FROM Proizvod WHERE ProzivodID = $PID
) b
SET a.Saldo = a.Saldo - b.Cena
WHERE a.KupuvacID = $KID

Outras dicas

Can't you just combine these into one query?

UPDATE Kupuvac 
SET Saldo = Saldo - (SELECT Cena FROM Proizvod WHERE ProzivodID = $PID) 
WHERE KupuvacID = $KID
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top