【问题标题】:Update column in one table based on value in another table in mysql根据mysql中另一个表中的值更新一个表中的列
【发布时间】:2016-07-09 06:23:22
【问题描述】:

我有一个名为“quot_items”的表,其中包含一个 grand_total 字段。它有这样的结构

tender_id |  grand_total
15001          100000
15002          250000
15003          1500000
16009          4500000

我有另一个名为“quotation”的表,其中包含一个“category”字段。它具有这种结构。

tender_id |  category
15001          H
15002          E
15003          B
16009          A

我正在尝试的是我需要一个 MYSQL 中的 UPDATE 语句,其中存在某些条件:

如果 [grand_total']

如果 (['grand_total'] >= '100000') && (['grand_total']

如果 (['grand_total'] > '200000') && (['grand_total']

如果 (['grand_total'] > '600000') && (['grand_total']

如果 (['grand_total'] > '1000000') && (['grand_total']

还有更多条件。我需要一个查询在 MYSQL 中执行此操作,以便我可以在一个更新语句中更新我的数据库。请任何人都可以帮助我。

我尝试了以下方法:

UPDATE quotation INNER JOIN quotation_items ON quotation.tender_id = quotation_items.tender_id
SET quotation.category = (
    CASE WHEN quotation_items.grand_total < 100000 then 'H' 
    WHEN quotation_items.grand_total >= 100000 && quotation_items.grand_total <= 200000 then 'G'
    WHEN quotation_items.grand_total > 200000 && quotation_items.grand_total <= 600000 then 'F'
    WHEN quotation_items.grand_total > 600000 && quotation_items.grand_total <= 1000000 then 'E'
    WHEN quotation_items.grand_total > 1000000 && quotation_items.grand_total <= 1500000 then 'D'
    END
);

【问题讨论】:

    标签: mysql sql-update case


    【解决方案1】:

    试试这个:

    UPDATE quotation t1
    INNER JOIN quot_items t2
    ON t1.tender_id = t2.tender_id
    SET t1.category = 
        CASE WHEN t2.grand_total < 100000 THEN 'H' 
        WHEN grand_total >= 100000 AND grand_total <= 200000 THEN 'G'
        WHEN grand_total > 200000 AND grand_total <= 600000 THEN 'F'
        WHEN grand_total > 600000 AND grand_total <= 1000000 THEN 'E'
        WHEN grand_total > 1000000 AND grand_total <= 1500000 THEN 'D'
        ELSE t1.category
        END
    

    【讨论】:

      【解决方案2】:

      使用CASE 表达式。

      查询

      SELECT tender_id,
      CASE WHEN grand_total < 100000 then 'H' 
      WHEN grand_total >= 100000 && grand_total <= 200000 then 'G'
      WHEN grand_total > 200000 && grand_total <= 600000 then 'F'
      WHEN grand_total > 600000 && grand_total <= 1000000 then 'E'
      WHEN grand_total > 1000000 && grand_total <= 1500000 then 'D'
      END AS Category
      FROM quot_items;
      

      如果您想更新category 列。那么,

      UPDATE quot_items
      SET category = (
          CASE WHEN grand_total < 100000 then 'H' 
          WHEN grand_total >= 100000 && grand_total <= 200000 then 'G'
          WHEN grand_total > 200000 && grand_total <= 600000 then 'F'
          WHEN grand_total > 600000 && grand_total <= 1000000 then 'E'
          WHEN grand_total > 1000000 && grand_total <= 1500000 then 'D'
          END
      );
      

      【讨论】:

      • 谢谢。是的,我正在寻找更新声明。但是我在这里使用 2 个表,意味着我正在验证 quot_items 表中的数据,我需要在报价表中更新。
      • 那你必须使用join
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      相关资源
      最近更新 更多