문제

Am I blind, or what is wrong with my query?

select 
    STRCMP( message, 'LogMessage') = 1
from 
    LogEntries;

works fine. However

select 
    IF STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF
from 
    LogEntries;

returns:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF from LogEntries' at line 2

What is wrong with this statement?

도움이 되었습니까?

해결책

You might want to use CASE WHEN:

SELECT
  CASE WHEN STRCMP( message, 'LogMessage') = 1 THEN 'bla' END AS your_column
FROM
  LogEntries;

when the condition is true it will return 'bla' otherwise, since there's no else part, it will return NULL.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top