2

Hello I have looked on these forums and I need help with getting name of column based on value.

For example I have a table such as:

|Name  | Column1 | Column2 | Column3|
|-----------------------------------|
|Peter | 100     | 120     | 92     |
|-----------------------------------|
|James | 110     | 105     | 88     |
|-----------------------------------|
|David | 90      | 112     | 98     |

I want to do a query of Peter where the value is 92 and I need the query to return Column3.

I know very basic sql and have not really used sqlite to understand it in depth.

I searched and the best I found was this thread which does have a solution that works on mysql: Get column name dynamically by Specific row value.

I tested this and it does exactly what I would like. Of course it's not for sqlite.

I need to know if this can be done using sqlite and if so, what is the proper syntax? Again I know very basic SQL.

If anyone has any tips/advice I appreciate it.

4

1 に答える 1

1

列が少なく、名前が固定されている場合は、これを使用できます:-

select case when col1 == 92 then col1 
            when col2 == 92 then col2 
            when col3 == 92 then col3
       end
  from tab;
于 2013-08-12T08:26:41.440 に答える