0

I am updating record on the base of sub query but it giving me error

you can't specify target table for update in from clause

my query is

UPDATE paymentinfo set customer_id = 
(
   SELECT transation.transactionid 
   FROM paymenttransaction AS transation 
   LEFT JOIN paymentinfo as payment 
   ON (transation.paymentinfoid=payment.paymentinfoid) 
   where payment.hash="0b576d33c57484692131471a847eab7c"
) 
WHERE hash="0b576d33c57484692131471a847eab7c"

where am i wrong and what will be perfect solution for that problem

Dharman
  • 30,962
  • 25
  • 85
  • 135
Umar Akbar
  • 123
  • 2
  • 7

2 Answers2

1

I think it is simplest (in your case) to use the double subquery method:

UPDATE paymentinfo 
    SET customer_id = (SELECT transactionid
                       FROM (SELECT t.transactionid
                             FROM paymenttransaction pt LEFT JOIN
                                  paymentinfo pi
                                  ON t.paymentinfoid = pi.paymentinfoid
                             WHERE p.hash = '0b576d33c57484692131471a847eab7c'
                            ) t
                      ) 
    WHERE hash = '0b576d33c57484692131471a847eab7c';

Usually, you want to switch these to use JOIN, but I think that is a bit complicated in this case.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

You are updating the table 'paymentinfo' also at same time you are using this table for selection in subquery . Please break this query in two parts and it will work .

Mukesh Swami
  • 415
  • 2
  • 11