Search code examples
mysqlignition

How to Change Select Query Value to Text using MySQL?


Take a look at the following query. How do you display Column 'Action' as text. If the result of 'Action' is LEQ 0 then dipslay the text "Crash" and if 'Action' is GRT 0 display the text "Hold"?

SELECT col1 AS Action
FROM vdk
WHERE t_stamp Between "{StartTime}" AND "{EndTime}"

Solution

  • Use CASE WHEN ... ELSE ... END and select from your set (query):

    SELECT *, (CASE WHEN Action <= 0 THEN 'Crash' ELSE 'Hold' END) as ActionText
    FROM (
        SELECT col1 AS Action
        FROM vdk
        WHERE t_stamp Between "{StartTime}" AND "{EndTime}"
    ) q