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,2000i 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..
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 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:
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
Related
I need to update a json value in a column as well as update another column in the same query.
Something like this:
UPDATE fixtures
SET jsonResults = '{}',
JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
How can I accomplish this?
JSON_SET() returns a JSON document value, but an UPDATE statement needs a series of assignment expressions:
UPDATE fixtures
SET jsonResults = '{}',
jsonFixture = JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
This replaces jsonFixture with the result of JSON_SET(), after setting a field within that document.
Compare with an UPDATE like this:
UPDATE mytable
SET i = i + 1
WHERE ...
It takes the value of i, adds 1, and then uses the result of that addition expression to replace i.
Please how do i update all record to 0 and set only the selected ID to 1
UPDATE address SET default_addr = 1
WHERE addr_id = 100 AND user = 'peter'
The above query will update the selected address to 1 which is good, but i want to set other address or the old selected default to 0 with one query
In MySQL, you can do:
UPDATE address
SET default_addr = (addr_id = 100 AND user = 'peter');
(This shorthand uses the fact that MySQL treats booleans as numbers in a numeric context, with "0" for false and "1" for true.)
If you want only one default address for the user named Peter, then use a where:
UPDATE address
SET default_addr = (addr_id = 100)
WHERE user = 'peter';
I suspect this is the logic that you really want.
use a conditional update using case statement
update address set default_address = case when addr_id = 100 and user = 'peter' then 1 else 0 end
here is a functional example
I built a sample schema. These are often helpful to provide in your future questions.
I'm new in MYSQL and I'm trying to do this:
UPDATE team
SET member1 = IF(member1=0, 001, SET member2 = IF(member2=0, 001, member2)) WHERE ID = ?;
This is not working, explain:
Each "member" has a different number (001, 002, 003...), I want to put the member "001" in the table "member1" but if this is ocupated (it has a value different of 0) set the member "001" in the table "member2" (a diferent table) but I think is not possible to put a SET inside of an IF. The id doesn't matters.
Thanks for attention ^^
No you can't join SET statements like this. It'll have to be worked around:
UPDATE `team`
SET
`member2` = CASE WHEN `member1` = 0 AND `member2` = 0 THEN '001' ELSE `member2` END,
`member1` = CASE WHEN `member1` = 0 THEN '001' ELSE `member1` END
WHERE
`id` = ?
/* optional filter: considering adding it if it does not bother the performance: */
AND (`member1` != 0 OR `member2` != 0)
;
Notes:
I used backticks around table and column names, I recommend you do so*
I used CASE/WHEN instead of IF, because IF is not ansi sql (it is specific to mysql)
You could also do two queries instead of one. That would allow you to use a filter in the where clause, so you don't have to update the column when it's not required.
*as pointed out by Bohemian, they are not ansi. I like to use them for the structural and visual help they provide.
I want to write a query that will verify 8 parameters of a row are the same as the parameters passed to the procedure. If any one of them is different then modify a status, otherwise do nothing.
Is there a way I can check all those parameters in a single IF case? For example:
IF (v_duty <> duty) OR (v_current <> current) OR (v_frequency <> frequency) THEN
* UPDATE ......;
END IF
Or do I have to use an ELSE IF for each comparison I want to make?
The above doesn't work, with or without brackets between each test.
You could probably manage this with a simple UPDATE and WHERE condition:
UPDATE table_name
SET status_column = 'new_status'
WHERE identifying_column = :identifier
AND (
v_duty != :v_duty
OR v_current != :current
OR v_frequency != :frequency
)
This is my previous question related to the my query.
MySQL select column which is a value in another column
The problem is that want to do operations on the values extracted and store it back into the original db. I've tried using a update & case but am not able to achieve it.
update msisdn_table
CASE reason
WHEN 'NoAnswer' THEN (case when (NoAnswer>0) then update msisdn_table set NoAnswer = NoAnswer-1 end)
WHEN 'NetworkBusy' THEN (case when NetworkBusy>0 then update msisdn_table set NetworkBusy = NetworkBusy-1 end)
WHEN 'CallRejection' THEN (case when CallRejection>0 then update msisdn_table set CallRejection = CallRejection-1 end)
WHEN 'Unavailable' THEN (case when Unavailable>0 then update msisdn_table set Unavailable = Unavailable-1 end)
END
Any help?
Try it this way if you want to do it one statement
UPDATE msisdn_table
SET NoAnswer = IFNULL(IF(reason = 'NoAnswer',
NULLIF(NoAnswer, 0) - 1, NoAnswer), 0),
NetworkBusy = IFNULL(IF(reason = 'NetworkBusy',
NULLIF(NetworkBusy, 0) - 1, NetworkBusy), 0),
CallRejection = IFNULL(IF(reason = 'CallRejection',
NULLIF(CallRejection, 0) - 1, CallRejection), 0),
Unavailable = IFNULL(IF(reason = 'Unavailable',
NULLIF(Unavailable, 0) - 1, Unavailable), 0)
WHERE reason IN('NoAnswer', 'NetworkBusy', 'CallRejection', 'Unavailable');
Note:
I changed CASE with less verbose IF(), although if you like it better you can use it the same way.
This approach has one possible side effect as it always updates the column(s) either with a new or with old value. It may matter if for example you have a trigger defined on the table.
You want to apply a WHERE clause to make sure that rows with other reason codes are not affected
Here is SQLFiddle demo
update msisdn_table set NoAnswer = NoAnswer-1 where (NoAnswer>0) ;
update msisdn_table set NetworkBusy = NetworkBusy-1 where (NetworkBusy = NetworkBusy-1) ;
update msisdn_table set CallRejection = CallRejection-1 where (CallRejection>0) ;
update msisdn_table set 'Unavailable' = 'Unavailable'-1 where (Unavailable>0) ;