-3

I have column location but i need to return this column based on value of another column below is the MYSQL SELECT QUERY:

if(block.block_id IS NOT NULL)
{
 location.location = 'BLOCKED'
}elseif(nvocc.nvocc_id is not null AND nvocc.nvocc_status = 'IN') {
  location.location = 'NVOCC'
}else {
  location.location = location.location
}

i treid with case conditon but its showing error

CASE 
     WHEN block.block_id IS NOT NULL THEN "BLOCKED"
     WHEN ((nvocc.nvocc_id IS NOT NULL) AND (nvocc.nvocc_status='IN')) THEN "NVOCC"
     ELSE location.location
END as location  

the above query returns nothing and not working please someone help me to solve this issue.

Jonathan John
  • 195
  • 2
  • 4
  • 12
  • 1
    What about you look at the [mySql tutorial](https://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html) – B001ᛦ Aug 28 '15 at 12:59
  • 3
    possible duplicate of [How do write IF ELSE statement in a MySQL query](http://stackoverflow.com/questions/8763310/how-do-write-if-else-statement-in-a-mysql-query) – Styphon Aug 28 '15 at 13:03
  • A query in MySQL starts with `SELECT`, not with `CASE`. It is entirely unclear what code you are actually running. – Gordon Linoff Aug 28 '15 at 13:34

1 Answers1

1

Try using parenthesis to clearly separate your condition block from control flow block:

CASE 
     WHEN block.block_id IS NOT NULL THEN "BLOCKED"
     WHEN ((nvocc.nvocc_id IS NOT NULL) AND (nvocc.nvocc_status='IN')) THEN "NVOCC"
     ELSE location.location
END

Try simplifying your query until it works and then progressively add conditions.

And please provide your error, it's really hard to guess.

Thomas
  • 63
  • 7