1

I am getting a syntax error in case statement in mysql. 1064- you have an error in your sql statement.

insert into
abc_table(release_date,title,detail,num)  
select model1.release_date,model1.title,model1.detail,model1.num
CASE 
WHEN model1.num= 0 THEN 3
WHEN model1.num= 1 THEN 1
WHEN model1.num= 2 THEN 2
END AS model1.num
from def_table  model1

Is this syntax is correct in mysql

4 Answers 4

1

You have two columns for model1.num:

insert into abc_table(release_date,title,detail,num)  
select model1.release_date,
   model1.title,
   model1.detail,
   model1.num -- <-- extra
   CASE 
      WHEN model1.num= 0 THEN 3
      WHEN model1.num= 1 THEN 1
      WHEN model1.num= 2 THEN 2
      END AS num
from def_table  model1

Your code should be:

insert into abc_table(release_date,title,detail,num)  
select model1.release_date,
   model1.title,
   model1.detail,
   CASE 
      WHEN model1.num= 0 THEN 3
      WHEN model1.num= 1 THEN 1
      WHEN model1.num= 2 THEN 2
      END AS num
from def_table  model1

Note: Sometimes, formatting your code will help find these errors. It can be difficult to see when all of the columns are in one row.

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

3 Comments

Pretty fast on the fiddle there bluefeet!
Thanks bluefeet but what if "num" datatype is enum then what?
@Java_NewBie I don't understand your question. Is this not working?
0

Try this,

you missed one comma

insert into
abc_table(release_date,title,detail,num)  
select model1.release_date,model1.title,model1.detail,
CASE 
WHEN model1.num= 0 THEN 3
WHEN model1.num= 1 THEN 1
WHEN model1.num= 2 THEN 2
END AS model1.num
from def_table  model1

Comments

0

you have an extra column in this select statement..

select model1.release_date,model1.title,model1.detail,model1.num

Comments

0
insert into abc_table(release_date,title,detail,num)  
select m.release_date
     , m.title
     , m.detail
     , CASE WHEN m.num = 0 THEN 3
            WHEN m.num= 1 THEN 1
            WHEN m.num= 2 THEN 2
        END num
  FROM def_table m;

Comments

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.