MySQL 数据透视表列数据作为行

2023-06-01数据库问题
4

本文介绍了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       |
+-----------+---------+--------+-------------+----------+----------+

任何帮助将不胜感激.

谢谢

推荐答案

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

  • SQLFiddle 演示
  • 如果您有未知数量的问题(特别是像 Matei Mihai 所说的 1000 个),则非常需要动态版本.

    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;
    

    • SQLFiddle 演示
    • 输出

      ╔════╦═════════╦════════╦═════════════╦══════════╦══════════╗
      ║ 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       ║
      ╚════╩═════════╩════════╩═════════════╩══════════╩══════════╝
      

      这篇关于MySQL 数据透视表列数据作为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

      The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12