Last day I came across a requirement where I had do share all the configs set for an entity to non-Engineering team - means, I need an Excel/CSV.
My table structure looks like
id | entity_id | config_id | value | is_active
And my resultant csv should look something like:
Entity_id, config1_value, config2_value, config3_value ...
I did get the output, but not with good ways. I am not convinced with the approach I have taken up. My query was like
SELECT entity_id,
group_concat(if(config_id = 1, value, "")) as config1_value,
group_concat(if(config_id = 2, value, "")) as config2_value,
group_concat(if(config_id = 3, value, "")) as config3_value,
...
FROM table_name
WHERE is_active = 1
AND config_id in (1,2,3...)
group by entity_id;
The query worked fine and as the table was still having less than 100K records, it was fast as well. As you see, if I need to add or delete new config key to report, I had to make changes to 2 line at the least. I am running MySQL 5.1 . What could be a better solution?