我正在使用mysql数据库和工具管理员,我想将数据从一个表转换为列并在json对象中获取它。 我有以下格式的表格 -
----------------------------------------------------
| prn | finding_field | finding_value | finding_no |
----------------------------------------------------
| 3 | Temperature | 100 | 1 |
| 3 | Fasting | 99 | 1 |
| 3 | Random | 120 | 1 |
| 6 | Temperature | 98 | 2 |
| 6 | cholesterol | 200 | 2 |
----------------------------------------------------
我的预期输出如下 -
-------------------------------------------------------------
| finding_no | Temperature | Fasting | Random | cholesterol |
-------------------------------------------------------------
| 1 | 100 | 99 | 120 | NULL |
| 2 | 98 | NULL | NULL | 200 |
-------------------------------------------------------------
我已经按如下方式编写了查询 -
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'GROUP_CONCAT(IF(finding_field = ''',
finding_field,
''',finding_value, NULL)) AS ',
finding_field
)
) INTO @sql;
SET @sql = CONCAT('SELECT finding_no
, ', @sql, '
FROM cpc_specific_finding
where prn=3
GROUP BY finding_no');
PREPARE stmt FROM @sql ;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt";
此查询在adminar中工作但在php中出错。
有没有办法在不使用prepare语句的情况下编写查询?