带连接的UPDATE列

时间:2010-12-15 12:25:15

标签: mysql sql mysql-error-1093

SELECT lott.id as lottery_id,lott.abbr,lott.currency,payments.id as payment_id,
    payment_prizes.prize_id,prizes.name,prizes.currency as prizes_currency
    FROM lotteries lott 
    JOIN lottery_payments payments 
        ON
        payments.lottery_id =  lott.id
    JOIN lottery_payment_prizes payment_prizes
        ON
        payment_prizes.payment_id = payments.id
    JOIN lottery_prizes prizes
        ON
        prizes.id =  payment_prizes.prize_id
    WHERE lott.currency = "ID"

我需要更新来自lottery_prizes表的所有货币WHERE id来自上面的查询。 我这样做了

UPDATE lottery_prizes SET currnecy = 'ID'
 WHERE id IN(SELECT prizes.id
    FROM lotteries lott 
    JOIN lottery_payments payments 
        ON
        payments.lottery_id =  lott.id
    JOIN lottery_payment_prizes payment_prizes
        ON
        payment_prizes.payment_id = payments.id
    JOIN lottery_prizes prizes
        ON
        prizes.id =  payment_prizes.prize_id
    WHERE lott.currency = "ID")

我有这个错误:
/ * SQL错误(1093):您无法在FROM子句* / 中为更新指定目标表'lottery_prizes' 谢谢大家

1 个答案:

答案 0 :(得分:6)

  UPDATE lottery_prizes l 
   JOIN (SELECT prizes.id
      FROM lotteries lott 
      JOIN lottery_payments payments 
          ON
          payments.lottery_id =  lott.id
      JOIN lottery_payment_prizes payment_prizes
          ON
          payment_prizes.payment_id = payments.id
      JOIN lottery_prizes prizes
          ON
          prizes.id =  payment_prizes.prize_id
      WHERE lott.currency = "ID") t
      ON t.Id = l.Id
 SET currency = 'ID'