1

Table1:-

ID  NAME    ADDRESS
1   TEST1    qwr
2   TEST2    sdf

I wanted to get column name in the select query based on ID column value

EX:-

If the Id=1 i want "name" column in select query if id=2 i want "address" column in select query

select name from table (when id=1)
select address from table(when id=2)

I hope i conveyed my query clearly.

0

2 Answers 2

2

This query will return the results in a single column and multiple rows:

SELECT CASE WHEN id = 1 THEN name WHEN id = 2 THEN address END AS result 
  FROM table;
Sign up to request clarification or add additional context in comments.

Comments

0

You can use subqueries to get the two results:

select (select name from table where id = 1) as name,
       (select address from table where id = 2) as address;

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.