Updating a field with the result of a select

2002-01-11 Thread Webolution

Hi,

Is it possible to update a field with the result of a select.  For example:

UPDATE ordr_order SET balance = ('SELECT ordr_order.total_cost -
sum(ordr_payment.amount) FROM ordr_order LEFT JOIN ordr_payment on
ordr_payment.ordr_order_id = ordr_order.id WHERE ordr_order.id = 1') WHERE
id = 1

When I execute this query, I don't get an error, but it also doesn't update
the field in question.  If I execute each query separately, they each work
fine.

Thanks for your help.

Daniel Leighton



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: Updating a field with the result of a select

2002-01-11 Thread Chris Bolt

 Is it possible to update a field with the result of a select.
 For example:

Try:

SELECT @newbalance := ordr_order.total_cost - sum(ordr_payment.amount) FROM
ordr_order LEFT JOIN ordr_payment on ordr_payment.ordr_order_id =
ordr_order.id WHERE ordr_order.id = 1;
UPDATE ordr_order SET balance = @newbalance WHERE id = 1;

 UPDATE ordr_order SET balance = ('SELECT ordr_order.total_cost -
 sum(ordr_payment.amount) FROM ordr_order LEFT JOIN ordr_payment on
 ordr_payment.ordr_order_id = ordr_order.id WHERE ordr_order.id = 1')
 WHERE id = 1

This doesn't work because it's setting balance to the text of the query, not
the result of the query itself. Since balance is probably not a string type,
it gets converted to a number which probably ends up making it zero. If
MySQL supported subselects it might work except without the single quotes
around the query.


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php