简体   繁体   中英

using nested case condition in mysql query

if(qtype==3)
  column=somecolumn;
else if(qtype==2)
    {
      if(open==0)
      column=anycolumn;
      else
      column =somecolumn;
    }
else{
acolumn,bcolumn,ccolumn  //this else----|
}                            |
             ----------------|

I want to achieve the above nested if-else condition in my mysql query.where the condition will be based on one of the column.this is what i have done so far.

select qtype,open,(Case when qtype=2 then answer
Else (Case when qtype=3 then 
                          (Case when open=0 then somecolumn  
                           Else othercolumn End) End)

Else....//how to implement this else here)

i am in confusion how to integrate the last else part?

Thanks

The format is correct, but you switch the 2 and 3 compared to the if-else psuedocode at the begining:

select qtype,open,(Case when qtype=3 then answer
Else (Case when qtype=2 then 
                          (Case when open=0 then somecolumn  
                           Else othercolumn End) End))

Looking at the case syntax , here is what I came up with.

CASE qtype
    WHEN 3 THEN column=somecolumn;
    WHEN 2 THEN (CASE open WHEN 0 THEN column=anycolumn ELSE column = somecolumn)
    ELSE othercolumn
END CASE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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