0

I can't get the result I want from the following query. Can I get a little help, please? thank you.

$sql = "SELECT id, 
                 CASE 
                    when Rcon = '' then '0',
                    when Rcon = 'x' then '1',
                    when Rcon = '2x' then '2',
                    when Rcon = 'x3' then '2',
                    when Rcon = 'x,x3' then '3',
                    when Rcon = 'x4' then '3' 
                    ELSE Rcon END AS Rcon
FROM mytable";
3
  • what do you want to do in else part? Commented Feb 1, 2019 at 13:56
  • In else part, I tried to give an option if there is any unexpected data. Commented Feb 1, 2019 at 13:57
  • if you just read the error message raised from mysql you can get what is the error. Commented Feb 1, 2019 at 15:08

1 Answer 1

1

You've got a problem with your CASE statement.

SELECT  id, 
        CASE 
          when Rcon = '' then '0'
          when Rcon = 'x' then '1'
          when Rcon = '2x' then '2'
          when Rcon = 'x3' then '2'
          when Rcon = 'x,x3' then '3'
          when Rcon = 'x4' then '3' 
          ELSE Rcon 
        END AS Rcon
  FROM mytable;

Note that I have removed the commas (,) from the end of each line of the CASE statement. Commas are not valid in a case statement.

Sign up to request clarification or add additional context in comments.

2 Comments

so just the comma does the trick? thank you, Martin. this works!
@Max Yes it's just the commas - you don't need them in a CASE statment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.