user2631534
user2631534

Reputation: 477

MySQL - How to read column value using variable name

I have table: BILL:

+-----------------
+ ID | b31 | b32 |
+-----------------
   1  1.99   4.67
------------------
   2  0.46   2.54
------------------

I im using this MySQL query to add 'b' to match column name in above table (from my previus query i get number 31) so this is what i use:

SELECT CONCAT('b', '31') FROM `bill` WHERE id=1;

I get output => b31 what is column name and i want to get that column value..so in this example i would like to get:

 +------------
 + ID | b31 |
 +------------
  1    1.99

I im beginner in MySQL and could not figure out why this is not working? Any help is welcome.

Thanks.

Upvotes: 0

Views: 74

Answers (1)

Strawberry
Strawberry

Reputation: 33935

A normalized design:

ID* ref* amount
 1  b31  1.99    
 1  b32  4.67
 2  b31  0.46   
 2  b32  2.54

( * = component of PRIMARY KEY)

Upvotes: 2

Related Questions