سؤال

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