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