How to write a MYSQL CASE WHEN statement with multiple search conditions? - mysql

I know languages like PHP has switch case control structure that supports multiple validations in a single case statement like,
Switch $x {
case 1,2,3:
$a = 0;
break;
case 5,6:
$a = 1;
break;
}
Similarly can this be done in MYSQL? I tried below, which really didn't work though :(
CASE vc_shape
WHEN ('02' OR '51') THEN SET dc_square_1 = dc_square_1 + dc_row_total;
WHEN ('06' OR '30' OR 83) THEN SET dc_square_2 = dc_square_2 + dc_row_total;
.....
.....
ELSE
BEGIN
END;
END CASE;
Any ideas how can I achieve this?

Use the other format for CASE statements:
CASE
WHEN vc_shape IN ('02', '51') THEN SET dc_square_1 = dc_square_1 + dc_row_total;
WHEN vc_shape IN ('06', '30', '83') THEN SET dc_square_2 = dc_square_2 + dc_row_total;
.....
.....
ELSE
BEGIN
END;
END CASE;

Related

MYSQL - UPDATE multiple fields based on single CASE statement

OBJECTIVE
I am looking for a way to update one or both fields using the same CASE statement
UPDATE vendor
SET special_cost =
(
CASE
WHEN
cost > 14
THEN
14
ELSE
SET special_cost = cost, cost = 14
END
)
WHERE upc = '12345678912345'
LOGIC
This is some logic that explains what I want to happen above.
if ($cost > 14) {
$special_cost = 14;
} else {
$special_cost = $cost;
$cost = 14;
}
It does not seem that there is a way to do this with a single CASE statement. Perhaps, this can be done with the mysql IF statement?
You are referring to case expressions not case statements.
You appear to want something like this:
UPDATE vendor
SET special_cost = GREATEST(cost, 14),
cost = 14
WHERE upc = '12345678912345';

How to properly program a "function" in verilog, for this specific example?

I have 3 values saved on Flip-Flops. During a certain state on a FSM I want to detect which is the bigger value and as a result of this, output into a memory a number.
In a side of the top-module file, I'm writing the function like this:
function [1:0] max_val;
input [7:-24] A, B, C;
begin
if (A > B)
begin
if (A > C)
max_val = 2'b01;
else
max_val = 2'b11;
end
else if(B > C)
max_val = 2'b10;
else
max_val = 2'b11;
else
max_val = 2b'00;
end
endfunction
Then during a state of the FSM I do this:
S13:
begin
case (max_val(FF_v1, FF_v2, FF_v3)) /// HERE??
01:
begin
mem_out1 = 1;
end
10:
begin
mem_out2 = 1;
end
11:
begin
mem_out3 = 1;
end
00:
begin
... /// what to do here??
end
endcase
end
I would like to ask if I'm defining and calling the function correctly, when I make use of the function, I understand I must use the same name of the function as "variable" and use it to define a case, right? So how do I define the input of that function, just like I did in "HERE???"
Also, if there are only 3 possible answers, and the one the combinations is unused, what to define in default??
The function declaration is right. Why the input [7:-24] changed it to input [23:0] . Also the if statement nesting was incorrect.
function [1:0] max_val;
input [23:0] A, B, C;
begin
max_val = 0 ;
if (A > B)
begin
if (A > C)
max_val = 2'b01;
else
max_val = 2'b11;
end
else
if(B > C)
max_val = 2'b10;
else
max_val = 2'b11;
end
endfunction
The case statement is fine . Though the values have to be prefixed with 2'b else it will assume the value to be decimal and not match. The case statement need not always have all the options. You can have only 3 or as many as you need . It is better to add a default statement. You can assign the values to x in the default or just print error for simulation purposes.
case ( max_val(a,b,c) )
2'b01 : begin
mem_out_1 = 1;
end
2'b10 : begin
mem_out_2 = 1;
end
2'b11 : begin
mem_out_3 = 1;
end
// no need for 00 case
default : begin
$display(" error val ");
end
endcase
http://www.testbench.in/TB_18_TASK_AND_FUNCTION.html
When I am coding this type of thing I like to "definitively" state what the outputs are going to be for each case, including the default, even if it is an error.
The point being that you could get a compiler that decides to only assign mem_out1 when you hit the 2'b01 state and leaves it set forever because nothing ever sets it back to zero. Not sure if that was the point of the design or if it might be undesired behavior.
case ( max_val(a,b,c) )
2'b01 : begin
{mem_out_3,mem_out_2,mem_out_1} = 3'b001;
end
2'b10 : begin
{mem_out_3,mem_out_2,mem_out_1} = 3'b010;
end
2'b11 : begin
{mem_out_3,mem_out_2,mem_out_1} = 3'b100;
end
// no need for 00 case
default : begin
// {mem_out_3,mem_out_2,mem_out_1} = 3'b000; // Uncomment if you need to control the output in the event of an error
$display(" error val ");
end
endcase

Error: duplicate case label free pascal compiler

I have the error "Error: duplicate case label", this is because i am using free pascal compiler, I have looked everywhere yet cannot find a solution, could you please provide me with one, thank you.
I will upload the full code in case there is something I am missing.
Sorry that it is messy.
program diceroll;
uses crt;
var count,time,double,dice1,dice2:integer;
sum1,sum2,sum3,sum4,sum5,sum6:integer;
idk:boolean;
Function Is_Double(d1,d2:integer):boolean;
begin
if d1 = d2 then
Is_Double:=true
else
Is_Double:=false;
end;
begin
randomize;
clrscr;
writeln('How many times do you want to roll the dice');
writeln(' ');
readln(time);
double:=0;
sum1:=0;
sum2:=0;
sum3:=0;
sum4:=0;
sum5:=0;
sum6:=0;
repeat
begin
dice1:=random(6)+1;
dice2:=random(6)+1;
idk:=Is_Double(dice1,dice2);
count:= count + 1;
if (idk = true) then
begin
double:= double + 1;
writeln(dice1,' ',dice2,' ','true');
end
else
writeln(dice1,' ',dice2,' ','true');
end;
if idk=true then
begin
case dice1 of
1:sum1:=sum1+1;
1:sum2:=sum2+1;
1:sum3:=sum3+1;
1:sum4:=sum4+1;
1:sum5:=sum5+1;
1:sum6:=sum6+1;
end;
until count = time;
writeln(double);
writeln(' ');
writeln(' ');
writeln(' ');
writeln(' ');
writeln(' Amount of doubles ');
writeln('1 2 3 4 5 6');
writeln(sum1,' ',sum2,' ',sum3,' ',sum4,' ',sum5,' ',sum6);
readln;
end.
Thank you
It's right here:
case dice1 of
1:sum1:=sum1+1;
1:sum2:=sum2+1;
1:sum3:=sum3+1;
1:sum4:=sum4+1;
1:sum5:=sum5+1;
1:sum6:=sum6+1;
It should be something like:
case dice1 of
1:sum1:=sum1+1;
2:sum2:=sum2+1;
3:sum3:=sum3+1;
4:sum4:=sum4+1;
5:sum5:=sum5+1;
6:sum6:=sum6+1;
Also your BEGIN... END structures look fishy to me:
repeat
// REPEAT doesn't need BEGIN
dice1:=random(6)+1;
dice2:=random(6)+1;
idk:=Is_Double(dice1,dice2);
count:= count + 1;
if (idk = true) then
begin
double:= double + 1;
writeln(dice1,' ',dice2,' ','true');
end
else
writeln(dice1,' ',dice2,' ','true');
// one extra END; removied - the one closing the unnecessory BEGIN at the start of REPEAT
if idk=true then
begin
case dice1 of
1:sum1:=sum1+1;
2:sum2:=sum2+1;
3:sum3:=sum3+1;
4:sum4:=sum4+1;
5:sum5:=sum5+1;
6:sum6:=sum6+1;
end; // CASE must have an END;
end;
until count = time;

Update table with case statement in mysql

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.

Help with MySQL Procedure Nested IF ELSE statments

Hi I need help with nested if else statements in MySQL. Please verify if the code below are the same? The C code is what I want to be accomplished in MySQL. I don't have syntax errors. But it seems that I am not getting the right result.
MySQL Stored Proc
IF top10_rank <= 10 AND top100_rank <=10 THEN SET temp_rank = 10;
ELSE SET temp_rank = 100;
END IF;
IF temp_rank = 10 THEN
IF top10_rank_date > top100_rank_date THEN SET rank = top10_rank;
ELSE SET rank = top100_rank;
END IF;
ELSEIF temp_rank = 100 THEN SET rank = top100_rank;
ELSE SET rank = 0;
END IF;
C code
if(top10_rank <= 10 && top100_rank <=10)
{
temp_rank = 10;
}
else
{
temp_rank = 100;
}
if(temp_rank == 10)
{
if(top10_rank_date > top100_rank_date)
{
rank = top10_rank
}
else
{
rank = top100_rank
}
}
else if(temp_rank == 100)
{
rank = top100_rank;
}
else
{
rank = 0;
}
It seems that the pieces are equivalent without regarding such things as size of integer (? may be float) fields and handling of NULL values in SQL.
Code looks not good:
1) This code is unreachable:
else
{
rank = 0;
}
2) It could be shortened - temp_rank could be inlined
3) Probably you need use this function is SELECT, it could be rewritten with CASE operator - to make calls more effective
4) To detect a problem, wrap the C and SQL pieces in functions and specify for which input parameters results are different.