使用连接更新列

发布于 2024-10-07 09:39:04 字数 1110 浏览 5 评论 0原文

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 表中的所有货币,其中 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' */ 谢谢大家

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"

I need to UPDATE all currency from lottery_prizes table WHERE id's are from query above.
I did this

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")

I have this error:
/* SQL Error (1093): You can't specify target table 'lottery_prizes' for update in FROM clause */
Thanks all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮云壑 2024-10-14 09:39:04
  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'          
  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'          
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文