3

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?

2

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.