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如何显示它:
then jus时间显示它,你将与一个正常的查询完成, 这是怎么了我usaly 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>';
@ nicholas--我想,不能让在格式的数据我想要。 – 2012-07-11 09:55:12