3

I've got a MySQL statement that's failing to compile, can anyone give me a basic example of how it should work? Semi pseudo-code:

IF (SELECT 'id' FROM terms WHERE name = 'thename' IS NULL) THEN
# Do this...
ELSE
# do this...
END IF

I just can't get any IF statement to work at all, not even a test one like:

IF(1=1) 
THEN SELECT "works"
END IF
3
  • 4
    Is this a SQL statement or a stored procedure? The form you're using here is only for stored procedures, as made clear in the documentation. Commented Sep 5, 2013 at 14:55
  • Most probably you are running it as MySQL query. Commented Sep 5, 2013 at 14:57
  • 1
    Depending on what your "Do this" bit is, you might be better off using the expression form of IF rather than the statement form. Commented Sep 5, 2013 at 14:59

1 Answer 1

6

For your first example, you're not evaluating the condition. You need to move the IS NULL outside the SELECT:

IF (SELECT 'id' FROM terms WHERE name = 'thename') IS NULL THEN
...

If you're running this inside a SELECT statement, you may want to use CASE:

SELECT
    CASE
        WHEN (SELECT 'id' FROM terms WHERE name = 'thename') IS NULL THEN ...
    END

Otherwise, see the IF documentation.

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.