在我的流体结构中,我有这个原始结果:
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[200];
printf("Enter name\n");
scanf("%s", &s);
int i;
for (i = 0; i < strlen(s); i++)
printf("%c\n", s[i]);
return 0;
}
改造后:
+----------------------------------------------------------------------+
| Results |
+----+----------+-------------+----------+-----------+-----------------+
| id | group_id | question_id | question | answer_id | answer | input |
+----+----------+-------------+----------+-----------+-----------------+
| 1 | 10001 | 1 | How old | 1 | 25 | NULL |
| 2 | 10001 | 2 | What like| 3 | Cola | NULL |
| 3 | 10001 | 2 | What like| 4 | Other | HotDog |
| 4 | 10001 | 3 | City | 5 | NYC | NULL |
| 5 | 10001 | 4 | Name | 7 | Other | Alex |
| 6 | 10002 | 1 | How old | 1 | 25 | NULL |
| 7 | 10002 | 2 | What like| 6 | Candy | NULL |
| 8 | 10002 | 3 | City | 8 | LA | NULL |
| 9 | 10002 | 4 | Name | 7 | Other | Roman |
+----+----------+-------------+----------+-----------+--------+--------+
我有这张桌子:
SET @i := 1;
SELECT @i := @i + 1 AS `id`,
GROUP_CONCAT(CASE WHEN question = 'How old' THEN answer ELSE NULL END) AS `How Old`,
GROUP_CONCAT(CASE WHEN question = 'What like' THEN IF(answer='Other', input, answer) ELSE NULL END) AS `What like`,
GROUP_CONCAT(CASE WHEN question = 'City' THEN answer ELSE NULL END) AS `City`,
GROUP_CONCAT(CASE WHEN question = 'Name' THEN IF(answer='Other', input, NULL) ELSE NULL END) AS `Name`
FROM theTable
GROUP BY group_id;
这是示例查询,但是具有正常行ids链接的真实查询 - 完美地运行。但是,我对其他quiez中的一些典型答案变体有不同的问题。
我可以改变这条规则:
+----+----------+-------------+----------+-----------+
| id | How Old | What like | City | Name |
+----+----------+-------------+----------+-----------+
| 1 | 25 | Cola,HotDog | NYC | Alex |
| 2 | 25 | Candy | LA | Roman |
+----+----------+-------------+----------+-----------+
到MySQL循环或顶级答案中的程序如https://dba.stackexchange.com/questions/47902/how-to-transpose-convert-rows-as-columns-in-mysql?
P.S。 另外,我可以在PHP中准备“select”,但它太简单=)