I have this code (I took it from other SO threads):
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(property_name = ''',
property_name,
''', value, NULL)) AS ',
property_name
)
) INTO @sql
FROM properties;
#INNER JOIN combustibles ON properties.property_name = combustibles.id_combustible
SET @sql = CONCAT('SELECT anio, mes, ', @sql, ' FROM properties GROUP BY anio, mes');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
And it's doing their job, my table is:
table: properties
id year month property_name value
1 1 ene a 345
2 1 ene b 545
3 1 ene c 65
4 1 feb a 32
5 1 feb b 57
6 1 feb c 444
7 2 ene a 123
8 2 ene b 333
9 2 ene c 12
10 2 feb a 56
11 2 feb b 565
12 2 feb c 34
and I'm getting what I need:
year month a b c
1 ene 345 545 65
1 feb 32 57 444
2 ene 123 333 12
2 feb 56 565 34
But the problem is if the values of "property_name" are only NUMBERS, like this:
table: properties
id year month property_name value
1 1 ene 1 345
2 1 ene 2 545
3 1 ene 3 65
etc...
I get this error:
Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '1,MAX(IF(property_name = '2', value, NULL)) AS 2,MAX(IF(property_name = '3', val' at line 1
I NEED that those "property_name" value be numbers, because this is an example, but with my real data, those values are id's from another table. Any hint?