我需要在另一个中使用一个mySQL查询的输出(它的值)
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!
我无法将一个查询的输出用作另一个查询的值。 问题是我需要使用以下查询来获得一个产品(成功完成)的价格: p>
SELECT Cena FROM Proizvod WHERE ProzivodID = $ PID
code> pre>
$ PID是一个PHP变量,这个查询工作正常。 当我想从买方的余额中减去产品的价格时出现问题。
我尝试使用以下查询来做到这一点: p>
UPDATE Kupuvac SET Saldo = Saldo - “。$ cena。” 在哪里KupuvacID = $ KID
code> pre>
$ cena是一个PHP变量,其值是第一个查询。 p>
第二个查询根本不起作用,它不会返回错误消息,但它不会改变买方的余额。 p>
A 短语如果有人需要它: Cena - Price code>; Proizvod - Product code>; Kupuvac - 买方 code>; Saldo - Balance code>。 em> p>
提前致谢! p>
div>
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
Can't you just combine these into one query?
UPDATE Kupuvac
SET Saldo = Saldo - (SELECT Cena FROM Proizvod WHERE ProzivodID = $PID)
WHERE KupuvacID = $KID