One possible way is to build a dynamic query, let's see this proof of concept:
create table if not exists informes(
username varchar(255),
inf_name varchar(255)
);
insert into informes(username, inf_name) values ( 'juan', 'INF_01');
insert into informes(username, inf_name) values ( 'pedro', 'INF_01');
insert into informes(username, inf_name) values ( 'juan', 'INF_02');
insert into informes(username, inf_name) values ( 'luis', 'INF_03');
SET @sumas = "";
select GROUP_CONCAT(CONCAT("sum(inf_name='", inf_name, "') AS ", inf_name) SEPARATOR ', ')
INTO @sumas
from ( select inf_name
from informes
group by inf_name
) G;
SET @query = CONCAT("select username, ", @sumas, " FROM informes GROUP BY username; ");
select @query;
If we review the variable @query
we see how we have the query:
select username, sum(inf_name='INF_01') AS INF_01, sum(inf_name='INF_02') AS INF_02, sum(inf_name='INF_03') AS INF_03 FROM informes GROUP BY username;
We can add line breaks to make it look better, but beyond that, it's basically what you were looking for, but built dynamically, the only thing that remains is to execute it, like this:
PREPARE stmt1 FROM @query;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
And we will obtain the desired output:
username INF_01 INF_02 INF_03
======== ====== ====== ======
juan 1 1 0
luis 0 0 1
pedro 1 0 0
Of course, all dynamic queries have limits:
- Variable size
@query
- Number of columns that a query can return
I do not know about MySql, I suggest you investigate them.