1

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
4

1 回答 1

5

对于您的第一个示例,您没有评估条件。您需要将IS NULL外部移动SELECT

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

如果您在SELECT语句中运行它,您可能需要使用CASE

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

否则,请参阅IF文档

于 2013-09-05T14:58:04.967 回答