Reputation: 7882
I have the following sql query:
SELECT id, first_name, last_name FROM users;
Now I want to add a case statement with the following clauses:
1. If 1+1 == 2 then show only id column
2. If 1+2 == 2 then show only first_name column
3. If 1+3 == 3 then show only last_name column
This is only stupid example but should describe what I'm looking for. Is there a way to do this in PostgreSQL?
Upvotes: 3
Views: 2916
Reputation: 176214
You could use CASE
:
SELECT CASE WHEN cond1 THEN id:TEXT
WHEN cond2 THEN first_name::TEXT
WHEN cond3 THEN last_name::TEXT
ELSE 'some_default_value'
END
FROM your_table;
Upvotes: 3