Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a specific column based on another column's value

I have a table like this

ID | Type | Val0 | Val1
1  |  0   |  A   | NULL
2  |  1   | NULL |  B

I need to select Val0 when the type is 0, and Val1 when the type is 1, and ValN when type is N...

How can I do that?

like image 365
BrunoLM Avatar asked Jul 26 '10 20:07

BrunoLM


People also ask

How do I get the value of a column based on another column value?

You can extract a column of pandas DataFrame based on another value by using the DataFrame. query() method. The query() is used to query the columns of a DataFrame with a boolean expression.

How do I select a specific column in a database?

To select columns, choose one of the following options: Type SELECT , followed by the names of the columns in the order that you want them to appear on the report. Use commas to separate the column names.


1 Answers

SELECT CASE
          WHEN Type = 0 THEN Val0
          WHEN Type = 1 Then Val1
          .
          .
          WHEN Type = N Then ValN
       END 
  FROM tbl
like image 111
dcp Avatar answered Oct 02 '22 23:10

dcp