MySQL first select column based on parameter value and then value of column

Ajhar Shaikh

I have a table lets say

Table1

t_id A   B  C

100  1   1  0

101  1   0  1

102  1   1  0

Now I have input parameter param0 based on which first I have to select column (lets say B) and then using t_id value(lets say 101) i will be selecting value of that selected column(which is 0).

I want to create single query as i will be using it in join query.

Is above possible, if yes how to write the sql for same?

Gordon Linoff

In MySQL, this is most easily done with a case statement:

select (case when @param0 = 'A' then A
             when @param0 = 'B' then B
             when @param0 = 'C' then C
        end) as val
from table t
where id = @t_id;

Actually, it is most easily done with a case statement in almost any database.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL first select column based on parameter value and then value of column

From Dev

MySQL, select column name based on a value

From Dev

mysql select query php based on column value

From Dev

SELECT Mysql - Replacing value in one column based on another column

From Dev

select mysql column based on occurrence and value in another column

From Dev

Select lines based on value in a column

From Dev

Conditional select based on column value

From Dev

Select lines based on value in a column

From Dev

SELECT with temporary column with value based on other column

From Dev

SELECT with temporary column with value based on other column

From Dev

MySQL - JOIN based on value of column?

From Dev

MySQL: Putting condition in SELECT query based on column value

From Dev

select mysql data and set value of a variable based on timestamp column

From Dev

select mysql data and set value of a variable based on timestamp column

From Dev

Select specific row from mysql table based on a column value

From Dev

MySQL SELECT incremental index based on value in a different column

From Dev

Add column to condition based on parameter value SQL

From Dev

Get the first instance of a record based on the value of a column

From Dev

Group words based on the value of the first column

From Dev

Update first header column based on checkbox value

From Dev

Return value pairs based on first row and column

From Dev

Remove entry based on the value of first column

From Dev

select the value of the first <field> in xml column in sql

From Dev

Select first row for each different column value

From Dev

Laravel - select rows with first occurance of column value

From Dev

MySQL Return the first non-NULL value from the list of the column in select statement with column name

From Dev

Mysql: change the column value based on column's previous value

From Dev

Mysql: change the column value based on column's previous value

From Dev

If duplicates exist, select the value based on another column

Related Related

  1. 1

    MySQL first select column based on parameter value and then value of column

  2. 2

    MySQL, select column name based on a value

  3. 3

    mysql select query php based on column value

  4. 4

    SELECT Mysql - Replacing value in one column based on another column

  5. 5

    select mysql column based on occurrence and value in another column

  6. 6

    Select lines based on value in a column

  7. 7

    Conditional select based on column value

  8. 8

    Select lines based on value in a column

  9. 9

    SELECT with temporary column with value based on other column

  10. 10

    SELECT with temporary column with value based on other column

  11. 11

    MySQL - JOIN based on value of column?

  12. 12

    MySQL: Putting condition in SELECT query based on column value

  13. 13

    select mysql data and set value of a variable based on timestamp column

  14. 14

    select mysql data and set value of a variable based on timestamp column

  15. 15

    Select specific row from mysql table based on a column value

  16. 16

    MySQL SELECT incremental index based on value in a different column

  17. 17

    Add column to condition based on parameter value SQL

  18. 18

    Get the first instance of a record based on the value of a column

  19. 19

    Group words based on the value of the first column

  20. 20

    Update first header column based on checkbox value

  21. 21

    Return value pairs based on first row and column

  22. 22

    Remove entry based on the value of first column

  23. 23

    select the value of the first <field> in xml column in sql

  24. 24

    Select first row for each different column value

  25. 25

    Laravel - select rows with first occurance of column value

  26. 26

    MySQL Return the first non-NULL value from the list of the column in select statement with column name

  27. 27

    Mysql: change the column value based on column's previous value

  28. 28

    Mysql: change the column value based on column's previous value

  29. 29

    If duplicates exist, select the value based on another column

HotTag

Archive