Search code examples
mysqlternary

Ternary in WHERE condition (MySQL)?


MySQL (5.7.x): Is there anyway to do a ternary argument in WHERE conditions?

I'm trying to run a AND clause based on the value of the select:

    SELECT DISTINCT p.user_id, eml.template, eml.subject
        FROM order_lines ol, email_template eml
        JOIN user p ON ol.user_id = p.user_od
        WHERE ol.status in (9.2, 10)
        -- This line here
            AND eml.name = (ol.status = 10 'SurveyOne' ? 'SurveyTwo')

I need to get user_id, and conditional row from eml based on ol.status value.


Solution

  • In MySQL, the ternary operator is the IF() function.

    AND eml.name = IF(ol.status = 10, 'SurveyOne', 'SurveyTwo')