I want to update multiple columns using case statement, I achieved this but it is not a best way i have to do same task three times, how can i achieve in one statement here is my test sql script:
Delimiter //
create procedure test_f()
begin
update test set
#### total####
test.total = case when test.currencyid='INR' then test.total/85.09
Else test.total End,
test.total = case when test.currencyid='SEK' then test.total/8.97
Else test.total End,
### Commission ####
test.commission = case when test.currencyid='INR' then test.commission/85.09
Else test.commission End,
test.commission = case when test.currencyid='SEK' then test.commission/8.97
Else test.commission End,
test.currencyid = case when test.currencyid in ('SEK','INR') then 'EUR'
Else test.currencyid End
WHERE test.currencyid in ('SEK','INR') ;
END //
Now i want to update all three columns altogether based on currencyid.
You could try this for the first case:
instead of:
update test set
#### total####
test.total = case when test.currencyid='INR' then test.total/85.09
Else test.total End,
test.total = case when test.currencyid='SEK' then test.total/8.97
Else test.total End,
change to:
update test set
#### total####
test.total =
case when test.currencyid='INR' then test.total/85.09
when test.currencyid='SEK' then test.total/8.97
Else test.total
End,
do the same for ### Commission ####
I would be inclined to write the query as:
update test t
set t.total = t.total / (case when t.currencyid = 'INR' then 85.09 else 8.97 end),
t.commission = t.commission / (case when t.currencyid = 'INR' then 85.09 else 8.97 end),
t.currency = 'EUR'
where t.currencyid in ('INR', 'SEK') ;
The where clause limits the currencies to the two mentioned, so the case only has to test for those two conditions. Simplifying the case statement also makes it clearer that the same logic is being used for both calculations.
Related
I would like to do the following.
Update a field based on the value of another field like
update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 fielde=3}
I know this is not valid mysql but its the best way for me to describe the problem.
update table set
b = case when a = 1 then 2 else b end,
c = case when a = 1 then 3 else c end,
d = case when a = 1 then d else 2 end,
e = case when a = 1 then e else 3 end
edit
according to your comment try this:
update table set
datefield_a = case when field_a = 1 then now() else datefield_a end,
datefield_b = case when field_a <> 1 then now() else datefield_b end
I think this syntax will achieve the result you attempted to specify.
UPDATE mytable
SET fieldb = CASE WHEN fielda = 1 THEN 2 ELSE fieldb END
, fieldc = CASE WHEN fielda = 1 THEN 3 ELSE fieldc END
, fieldd = CASE WHEN fielda = 1 THEN fieldd ELSE 2 END
, fielde = CASE WHEN fielda = 1 THEN fielde ELSE 3 END
The "trick" here is that we are updating all four columns, but in some "cases", we are assigning the current value of the column back to the column, resulting in no real change to the column value. (Once you get your mind bent around that idea, it's pretty easy.)
With MySQL, we do have a handy IF function (not available in most other RDBMS) that we can use to abbreviate that a bit, and achieve the same thing:
UPDATE mytable
SET fieldb = IF(fielda = 1, 2, fieldb)
, fieldc = IF(fielda = 1, 3, fieldc)
, fieldd = IF(fielda = 1, fieldd, 2)
, fielde = IF(fielda = 1, fielde, 3)
The pain is that you still have to repeat that same conditional test multiple times.
A single scan through the table (like these statements do), and getting all those assignments done in one fell swoop is going to be faster (and more efficient) than breaking this up and doing the assignments piecemeal using multiple statements.
I need to make a select like,
Select * from table WHERE column = x if column != -1
but i have no idea for now.
Anyone know or made in past something like that?
Thanks.
You should also write like this,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
You can utilize case when in where clause.
Similarly you can add more conditional criteria like,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
AND
1 = case when column1 [conditional operator] value then
case when column1 = xx then 1 else 0 end
else 1 end
This is just an example how you can integrate more conditional criteria together, even though you can add more case when in else part even.
What am I doing wrong, I keep getting errors about syntax.
UPDATE common_lookup
SET common_lookup_column =
CASE
WHEN common_lookup_table = 'MEMBER' THEN
CASE
WHEN common_lookup_type IN ('INDIVIDUAL', 'GROUP') THEN
common_lookup_table || '_TYPE'
WHEN common_lookup_type LIKE '%CARD' THEN
'CREDIT_CARD_TYPE'
END
ELSE
common_lookup_table || '_TYPE'
END;
In MySQL, using the concat() function:
UPDATE common_lookup
SET common_lookup_column = (CASE WHEN common_lookup_table = 'MEMBER'
THEN (CASE WHEN common_lookup_type IN ('INDIVIDUAL', 'GROUP')
THEN concat(common_lookup_table, '_TYPE')
WHEN common_lookup_type LIKE '%CARD'
THEN 'CREDIT_CARD_TYPE'
END)
ELSE concat(common_lookup_table, '_TYPE')
END);
Assuming that you don't intend to have NULL values from the inner case, you can simplify this logic to:
UPDATE common_lookup
SET common_lookup_column = (CASE WHEN common_lookup_table = 'MEMBER' AND
common_lookup_type LIKE '%CARD'
THEN 'CREDIT_CARD_TYPE'
ELSE concat(common_lookup_table, '_TYPE')
END);
The operation || is the string concatenation operator in several databases.
I would like to do the following.
Update a field based on the value of another field like
update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 fielde=3}
I know this is not valid mysql but its the best way for me to describe the problem.
update table set
b = case when a = 1 then 2 else b end,
c = case when a = 1 then 3 else c end,
d = case when a = 1 then d else 2 end,
e = case when a = 1 then e else 3 end
edit
according to your comment try this:
update table set
datefield_a = case when field_a = 1 then now() else datefield_a end,
datefield_b = case when field_a <> 1 then now() else datefield_b end
I think this syntax will achieve the result you attempted to specify.
UPDATE mytable
SET fieldb = CASE WHEN fielda = 1 THEN 2 ELSE fieldb END
, fieldc = CASE WHEN fielda = 1 THEN 3 ELSE fieldc END
, fieldd = CASE WHEN fielda = 1 THEN fieldd ELSE 2 END
, fielde = CASE WHEN fielda = 1 THEN fielde ELSE 3 END
The "trick" here is that we are updating all four columns, but in some "cases", we are assigning the current value of the column back to the column, resulting in no real change to the column value. (Once you get your mind bent around that idea, it's pretty easy.)
With MySQL, we do have a handy IF function (not available in most other RDBMS) that we can use to abbreviate that a bit, and achieve the same thing:
UPDATE mytable
SET fieldb = IF(fielda = 1, 2, fieldb)
, fieldc = IF(fielda = 1, 3, fieldc)
, fieldd = IF(fielda = 1, fieldd, 2)
, fielde = IF(fielda = 1, fielde, 3)
The pain is that you still have to repeat that same conditional test multiple times.
A single scan through the table (like these statements do), and getting all those assignments done in one fell swoop is going to be faster (and more efficient) than breaking this up and doing the assignments piecemeal using multiple statements.
What I have is a bit column NonMileage and based on that bit column i want to make a variable that I can use inside of a where clause.
this is a two part question:
How do you case a variable? The code below does not case the
#NoMileageListing
And then I have it setting #MileListingClause as a string, can I
just use #MileListingClause like where #MileListingClause?
.
SET #NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT
#NoMileageListing CASE #NoMileageListing when 1 then
SET #MileListingClause = 'tblcargarage.miles >= 0' else
SET #MileListingClause = 'tblcargarage.miles != 0' end case;
here's the answer
SET #NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT CASE #NoMileageListing
WHEN 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO #NoMileWhereClause;
select #NoMileWhereClause;
found here:
Mysql Storing a variable with the result of an SELECT CASE
I think the problem is located in the when part.
It should be:
SELECT CASE #NoMileageListing
WHEN #NoMileageListing = 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO #NoMileWhereClause;
select #NoMileWhereClause;
to create the dynamic query with the #noMileWhereClause refer to the answer here:
How To have Dynamic SQL in MySQL Stored Procedure