mysql的列数据作为行,MySQL数据透视表的列数据作为行

I'm struggling to find a solution this MySQL problem. I just can't seem to get my head around how to do it. I have the following tables.

Question table

+----+-------------+

| id | question |

+----+-------------+

| 1 | Is it this? |

| 2 | Or this? |

| 3 | Or that? |

+----+-------------+

Results Table

+----+---------+--------+

| id | user_id | job_id |

+----+---------+--------+

| 1 | 1 | 1 |

| 2 | 1 | 3 |

| 3 | 2 | 3 |

+----+---------+--------+

Answers table

+----+-------------------------+--------------+

| id | answer | fk_question_id | fk_result_id |

+----+-------------------------+--------------+

| 1 | Yes | 1 | 1 |

| 2 | No | 2 | 1 |

| 3 | Maybe | 3 | 1 |

| 4 | Maybe | 1 | 2 |

| 5 | No | 2 | 2 |

| 6 | Maybe | 3 | 2 |

| 7 | Yes | 1 | 3 |

| 8 | Yes | 2 | 3 |

| 9 | No | 3 | 3 |

+----+-------------------------+--------------+

If possible I'd like to display the question answers as columns for each result set, like this.

+-----------+---------+--------+-------------+----------+----------+

| result_id | user_id | job_id | Is it this? | Or this? | Or that? |

+-----------+---------+--------+-------------+----------+----------+

| 1 | 1 | 1 | Yes | No | Maybe |

| 2 | 1 | 3 | Maybe | No | Maybe |

| 3 | 2 | 3 | Yes | Yes | No |

+-----------+---------+--------+-------------+----------+----------+

Any help would be much appreciated.

Thanks

解决方案SELECT a.ID,

a.user_ID,

a.job_id,

MAX(CASE WHEN c.question = 'Is it this?' THEN b.answer END) 'Is it this?',

MAX(CASE WHEN c.question = 'Or this?' THEN b.answer END) 'Or this?',

MAX(CASE WHEN c.question = 'Or that? ' THEN b.answer END) 'Or that? '

FROM Results a

INNER JOIN Answers b

ON a.id = b.fk_result_id

INNER JOIN Question c

ON b.fk_question_id = c.ID

GROUP BY a.ID,

a.user_ID,

a.job_id

If you have unknow number of questions (specifically 1000 like Matei Mihai said), a dynamic version is much required.

SET @sql = NULL;

SELECT

GROUP_CONCAT(DISTINCT

CONCAT(

'MAX(CASE WHEN c.question = ''',

question,

''' then b.answer end) AS ',

CONCAT('`',question,'`')

)

) INTO @sql

FROM Question;

SET @sql = CONCAT('SELECT a.ID,

a.user_ID,

a.job_id, ', @sql, '

FROM Results a

INNER JOIN Answers b

ON a.id = b.fk_result_id

INNER JOIN Question c

ON b.fk_question_id = c.ID

GROUP BY a.ID,

a.user_ID,

a.job_id');

PREPARE stmt FROM @sql;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

OUTPUT

╔════╦═════════╦════════╦═════════════╦══════════╦══════════╗

║ ID ║ USER_ID ║ JOB_ID ║ IS IT THIS? ║ OR THIS? ║ OR THAT? ║

╠════╬═════════╬════════╬═════════════╬══════════╬══════════╣

║ 1 ║ 1 ║ 1 ║ Yes ║ No ║ Maybe ║

║ 2 ║ 1 ║ 3 ║ Maybe ║ No ║ Maybe ║

║ 3 ║ 2 ║ 3 ║ Yes ║ Yes ║ No ║

╚════╩═════════╩════════╩═════════════╩══════════╩══════════╝

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值