Это простой подход к созданию динамических c полей.
Заданная таблица:
mysql> select * from dyna_field;
+------+---------------+
| id | field_name |
+------+---------------+
| 1 | Gross Gallons |
| 2 | Net Gallons |
| 3 | Total |
+------+---------------+
Создание динамических c запросов:
select
group_concat(concat('(case when id = ', id, ' then field_name else "" end) as ID_', id, ' ')) into @sql_case
from dyna_field
join(SELECT @sql_case:='') c;
set @sql_case = concat('select ', @sql_case, ', 1 as uid from dyna_field;');
prepare statement FROM @sql;
execute statement;
+---------------+-------------+-------+-----+
| ID_1 | ID_2 | ID_3 | uid |
+---------------+-------------+-------+-----+
| Gross Gallons | | | 1 |
| | Net Gallons | | 1 |
| | | Total | 1 |
+---------------+-------------+-------+-----+