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

∥☆過路亽.° 提交于 2019-12-24 02:04:59

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/18927247/how-to-use-if-else-condition-in-a-select-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!