Search code examples
mysqlselectif-statement

How to use if/else condition in a select in mysql


How can I use an if/else condition on a select statement in mysql?

If Type = "Initial Read", Select the Initial Column from table_name else if Type = "Final Read", Select the Final Column from table_name

How should I do this? And what is the right query for this? Do i use case select?


Solution

  • Use CASE

    SELECT CASE Type
            WHEN "Initial Read" THEN Initial_Column
            WHEN "Final Read" THEN Final_Column
           END column
    FROM table_name
    WHERE <some condition>