I have this Mysql scenario
table1
--------------
id `7` `8`
--------------
1 10 20
2 20 30
table 2
---------------
id code rel
---------------
3 7 1
4 8 2
I am using following statement to get values
SELECT t2.id, t2.code,
CASE t2.code WHEN 7 THEN (SELECT `7` FROM table1 t1 where t1.id = t2.rel)
CASE t2.code WHEN 8 THEN (SELECT `8` FROM table1 t1 where t1.id = t2.rel)
END as val
FROM table2 t2
but it's neither pretty or functional, because I need these values summed, multiplied, etc, and there are a lot of columns.
Is there a way to join these tables and get table1.7
value for t2.rel,t2.code values?
Something similar to
SELECT t2.id, t2.code, eval(t1.(t2.code)) as val
FROM table2 t2
JOIN table1 t1 on t2.rel = t1.id
Thank you very much!