【问题标题】:Mysql query for displaying data用于显示数据的 Mysql 查询
【发布时间】:2012-07-11 09:42:07
【问题描述】:

我有一个这样的数据库:

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 

我想以如下格式显示以上数据的内容-

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

如何使用 sql 实现这一点?我正在使用mysql和php。

【问题讨论】:

  • @nicholas-- 我试过了,但无法以我想要的格式获取数据。

标签: php jquery mysql database


【解决方案1】:

您正在寻找 PIVOT 表:

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

【讨论】:

  • 非常感谢 PoeHah,这正是我所需要的。
【解决方案2】:
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

编辑 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>';

【讨论】:

  • 问题的数量可能是25-30,可能有100个用户,上面的查询会不会有性能问题??谢谢你的回答......
  • Pivot 会更好。对于此查询,您需要知道有多少问题
  • @Neeraj 我认为 php 解决方案可能更快,并且至少更动态,请参阅更新的答案
  • @PoeHaH 我阅读了您建议的链接,它描述了如何像我一样构建一个支点,我看不出有什么不同,那么它如何更好?
  • @PugganSe 感谢您的解决方案,但我有点困惑,如何使用上述 php 解决方案以表格格式(我在问题中提到)显示数据?
【解决方案3】:

试试这个...

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

【讨论】:

    【解决方案4】:
        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 
    

    【讨论】:

      【解决方案5】:

      如果您确定只有 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
      

      【讨论】:

        猜你喜欢
        • 2015-03-02
        • 2014-05-26
        • 1970-01-01
        • 1970-01-01
        • 2010-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多