简体   繁体   English

MySQL查询显示数据

[英]Mysql query for displaying data

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? 如何使用sql实现此目的? I am using mysql and php. 我正在使用mysql和php。

You're looking for a PIVOT table: 您正在寻找一个PIVOT表:

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

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 编辑1
added a php version, see comment 添加了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'];
}

Edit 2 how to display it: 编辑2如何显示它:

then just display it as you would done with a normal query, this is how i usaly converts php arrays to html: 然后像显示普通查询一样显示它,这就是我通常将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>';

Try this... 尝试这个...

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

If you are sure of only 3 fields you can use something like this. 如果您确定只有3个字段,则可以使用类似的内容。 Do remember that this query is quite heavy so you may be better of processing this in PHP instead.. 请记住,此查询非常繁琐,因此您最好在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
    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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM