0
insert into foo_table (fname, lname, number)
values ('John', 'Doe', if(123 = 456));

For the above MySQL query, can somebody kindly explain what the if(123 = 456) is doing? I currently struggle to see an if statement without a body (i.e. if(condition){ // do something });

3
  • It would insert a record with the values ('John', 'Doe', 0). As for the reason to have this, you will need to ask the person who wrote it. Commented Dec 20, 2016 at 8:11
  • It's a syntax error. The IF() function requires 3 arguments. The first argument is a condition expression. If it's true the function returns the second argument, otherwise it returns the third argument. Commented Dec 20, 2016 at 8:25
  • See dev.mysql.com/doc/refman/5.7/en/… Commented Dec 20, 2016 at 8:26

1 Answer 1

1

The query is syntactically not correct as per mysql version 8, The syntactically correct query is insert into foo_table (fname, lname, number) values ('John', 'Doe', if(123 = 456,1,2)). This will insert 1 if the condition (123 =456) is true, otherwise it will insert 2.

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

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.