Reputation: 307
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?
Upvotes: 2
Views: 6344
Reputation: 53
Suppose your Type field have only two value so you can use following query
SELECT IF(Type = "Initial Read", Initial_Column, Final_Column) AS column
FROM table_name
If Type field have more than two value to check so can use following query
SELECT IF(Type = "Initial Read", Initial_Column, IF (Type = "Final Read", Final_Column, Middle_Column)) AS column
FROM table_name
Upvotes: 0
Reputation: 781068
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>
Upvotes: 2