虽然我被告知有86行,但我看不到结果。我想我错过了一些非常简单但我无法找到的东西。
SET @sql_dynamic = (
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
' MAX(IF(exam_id = ''',
exam_id,
''', percent_correct, 0)) AS exam',
exam_id
))
FROM table1);
SET @sql = CONCAT('SELECT usuario, ',@sql_dynamic,' FROM table1 group by usuario');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
相当于
SELECT usuario, MAX(IF(exam_id = '2', percent_correct, 0)) AS exam2,
MAX(IF(exam_id = '3', percent_correct, 0)) AS exam3,
MAX(IF(exam_id = '4', percent_correct, 0)) AS exam4,
MAX(IF(exam_id = '5', percent_correct, 0)) AS exam5,
MAX(IF(exam_id = '6', percent_correct, 0)) AS exam6,
MAX(IF(exam_id = '7', percent_correct, 0)) AS exam7
FROM table1 GROUP BY usuario
这些是我的结果:
PREPARE stmt FROM @sql;# MySQL ha devuelto un conjunto de valores vacío (es decir: cero columnas).
EXECUTE stmt;# Filas: 86 <---- I am getting 86 rows but doesnt show them
DEALLOCATE PREPARE stmt;# MySQL ha devuelto un conjunto de valores vacío (es decir: cero columnas).
我有86行,但没有显示它们。我怎么能看到它们?谢谢。保存为新表也可以。
EDIT 如果我选择@sql我得到
SELECT usuario, MAX(IF(exam_id = '2', percent_correct, 0)) AS t2, MAX(IF(exam_id = '3', percent_correct, 0)) AS t3,
MAX(IF(exam_id = '4', percent_correct, 0)) AS t4,
MAX(IF(exam_id = '5', percent_correct, 0)) AS t5,
MAX(IF(exam_id = '6', percent_correct, 0)) AS t6,
MAX(IF(exam_id = '7', percent_correct, 0)) AS t7
FROM table1 group by usuario