0

I have a database like this:

id        User_id    Question_id     Answer
1           john      Question_1        b
2           john      Question_2        a
3           john      Question_3        d
4           harry     Question_1        a
5           harry     Question_2        a  
6           harry     Question_3        c 
7           colleen   Question_1        c
8           colleen   Question_2        a
9           colleen   Question_3        b 

I want to display the content of the above data in the following format-

id     User_id      Question_1      Question_2        Question_3       
1       john             b               a                 d
2       harry            a               a                 c
4       colleen          c               a                 b

How can I achieve this using sql? I am using mysql and php.

4

5 回答 5

3

您正在寻找 PIVOT 表:

http://en.wikibooks.org/wiki/MySQL/Pivot_table

于 2012-07-11T09:57:47.767 回答
1
SELECT
  id,
  User_id,
  MAX(IF(querstion_id = 'Question_1', answer, NULL)) AS Question_1,
  MAX(IF(querstion_id = 'Question_2', answer, NULL)) AS Question_2,
  MAX(IF(querstion_id = 'Question_3', answer, NULL)) AS Question_3
FROM answers
GROUP BY User_id

EDIT 1
添加了一个 php 版本,见评论

$table = array();
$query = "SELECT * FROM answers ORDER BY User_id, Question_id";

... fetch data depending on what interface you use ...

foreach/while(....)
{
   if(!isset($table[$result['User_id']]))
   {
       $table[$result['User_id']] = array();
       $table[$result['User_id']]['id'] = $result['id'];
       $table[$result['User_id']]['User_id'] = $result['User_id'];
   }

   $table[$result['User_id']][$result['Question_id']] = $result['Answer'];
}

编辑2如何显示它:

然后像使用普通查询一样显示它,这就是我通常将 php 数组转换为 html 的方式:

echo '<table><thead>';
echo '<tr><th>' . implode('</th><th>', array_keys(current($table))) . '</th></tr>';
echo '</thead><tbody>';
foreach($table as $row)
{
    echo '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
}
echo '</tbody></table>';
于 2012-07-11T10:11:00.570 回答
0

尝试这个...

SELECT tbl.user_id,
       (SELECT answer
          FROM your_table
         WHERE user_id = tbl.user_id AND question_id = 'Question_1') q1,
       (SELECT answer
          FROM your_table
         WHERE user_id = tbl.user_id AND question_id = 'Question_2') q2,
       (SELECT answer
          FROM your_table
         WHERE user_id = tbl.user_id AND question_id = 'Question_3') q3
  FROM (SELECT DISTINCT user_id
                   FROM your_table) tbl
于 2012-07-11T10:03:15.990 回答
0

如果您确定只有 3 个字段,则可以使用类似的内容。请记住,此查询非常繁重,因此您最好在 PHP 中处理它。

select a.user_id, 
a.question_id as Question_1, 
b.question_id as Question_2, 
c.question_id as Question_3 

from TABLE_NAME a, 
TABLE_NAME b, 
TABLE_NAME c  

where a.question_id="Question_1" and a.user in(select user_id from TABLE_NAME) 
and b.question_id="Question_2" and b.user_id =a.user_id 
and c.question_id="Question_3" and c.user_id =a.user_id
于 2012-07-11T10:06:33.507 回答
0
    select id,User_id,
    sum(Answer*(1-abs(sign(Question_id-1)))) as Question_1,
    sum(Answer*(1-abs(sign(Question_id-2)))) as Question_2,
    sum(Answer*(1-abs(sign(Question_id-3)))) as Question_3,
    sum(Answer*(1-abs(sign(Question_id-4)))) as Question_4
    from results group by User_id 
于 2012-07-11T10:11:08.667 回答