简体   繁体   English

大小写表达式以更改列的值

[英]case expression to change the value of a column

I have two columns are measurement_unit and measurement_size .The values in measurement_unit column are ml,gm,litre .And measurement_size has the values 200,1300,2000 i have converted all values of measurement_size to round figure such as .2,1.3,2 now i want to change the values of measurement_unit to liter or kg and here is my case expression .But it is not working..what should i do?In addition it is not a update query i am doing this for my report..it will not change any value in database.. 我有两列measurement_unitmeasurement_size在.The值measurement_unit柱是ml,gm,litre 。而measurement_size具有值200,1300,2000我已经转换的所有值measurement_size到圆图如.2,1.3,2现在我想将measurement_unit的值更改为literkg ,这是我的情况表达式。但是它不起作用..我该怎么办?此外,这不是更新查询,我正在为我的报告进行此操作。不会更改数据库中的任何值。

 CASE WHEN MUC.name='ml' THEN (MUC.name)='Liter' 
    WHEN MUC.name='gm' THEN (MUC.name)='Kg' 
    END AS Measurement,

One possibility would be to use a CASE WHEN inside an UPDATE statement: 一种可能是在UPDATE语句中使用CASE WHEN

UPDATE MUC
SET MUC.name = CASE
    WHEN MUC.name = 'ml' THEN 'Liter';
    WHEN MUC.name = 'gm' THEN 'Kg';
ELSE MUC.name
END

The only thing I don't like about this solution is that it will still update a row which does not match, using the current value. 我对此解决方案唯一不满意的是,它仍将使用当前值更新不匹配的行。 If you really only want to update the rows which you intend to convert, then you can try using two UPDATE statements, wrapped in a transaction: 如果您确实只想更新要转换的行,则可以尝试使用包装在事务中的两个UPDATE语句:

START TRANSACTION
    UPDATE MUC
    SET MUC.name = 'Liter' WHERE MUC.name = 'ml'
    UPDATE MUC
    SET MUC.name = 'Kg' WHERE MUC.name = 'gm'
COMMIT

The transaction might be necessary if you want to ensure that no one ever sees your units in an intermediate state. 如果要确保没有人看到您的单元处于中间状态,则可能需要进行事务处理。

You can try this. 你可以试试看

select 
CASE WHEN M_unit='ml' THEN 'Liter' 
    WHEN M_unit.name='gm' THEN 'Kg'
    ELSE M_UNIT 
    END AS Measurement_UNIT,
M_size/1000 as Measurement_SIZE
 from Table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM