我有一个数据库查询输出我需要的数据,但我不知道如何将其变为我需要的格式(也需要将其转换为csv格式,但需要首先获得输出)。
SELECT `user_id`, `assessment_name`, `question_id`, `question_type_id`, `answer`, `created_at` FROM answer WHERE `assessment_id` = 1
输出是这样的(虽然每次提交有30行 - 这里只放三个作为例子):
11 Three Intelligences 31 6 4 7/22/08 11:30
11 Three Intelligences 40 4 4 7/22/08 11:30
11 Three Intelligences 41 6 5 7/22/08 11:30
10 Three Intelligences 31 6 3 7/22/08 14:54
10 Three Intelligences 40 4 4 7/22/08 14:54
10 Three Intelligences 41 6 4 7/22/08 14:54
12 Three Intelligences 31 6 4 7/29/08 10:31
12 Three Intelligences 40 4 4 7/29/08 10:31
12 Three Intelligences 41 6 4 7/29/08 10:31
我需要的是重新格式化它以便它可以用于数据分析,这意味着为每个user_id和assessment_name和created_at
将数据分成一行assessment_name, user_id, answer(for question 31), question_type_id(for question 31), answer(for question 40), question_type_id(for question 40), answer(for question 41), question_type_id(for question 41), created_at
Three Intelligences, 11, 6, 4, 4, 4, 6, 5, 7/22/08 11:30
Three Intelligences, 10, 6, 3, 4, 4, 6, 4, 7/22/08 14:54
Three Intelligences, 12, 6, 4, 4, 4, 6, 4, 7/29/08 10:31
这可能吗?如果是这样,我也假设我可以在php中完成它,但我不知道足以找出必要的'while'循环。它甚至可能不需要生成csv文件输出...如果我可以在页面上正确格式化它,我可以复制/粘贴此特定项目。
由于
- EDIT-- 这是我到目前为止所得到的 - 这将生成数据的半原始输出......我认为我需要的只是找出'while'中需要循环内的循环以获得所需的输出
<?php
require_once '../connection.php';
// Create connection
$conn = new mysqli(localhost, $dbuser, $dbpass, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query_responses = "SELECT `user_id`, `assessment_name`, `question_id`, `question_type_id`, `answer`, `created_at` FROM answer WHERE `assessment_id` = 1";
$result_responses = $conn->query($query_responses);
if ($result_responses->num_rows > 0) {
// output data of each row
while($row = $result_responses->fetch_assoc()){
echo $row["created_at"]. ", " .$row["assessment_name"]. ", " .$row["user_id"]. "," .$row["question_id"]. "," .$row["question_type_id"]. "," .$row["answer"]. "<br />";
}
} else {
echo "0 results";
}
$conn->close();
?>
答案 0 :(得分:0)
这就是你要找的东西:
SELECT
`user_id`,
`assessment_name`,
CONCAT(
GROUP_CONCAT(
CONCAT(`question_type_id`, ', ', `answer`)
SEPARATOR ', ' ), ', ', `created_at`) AS RESULT
FROM answer
WHERE `assessment_id` = 1
GROUP BY `user_id`
ORDER BY `user_id`;
<强>示例强>
MariaDB []> SELECT
-> `user_id`,
-> `assessment_name`,
-> CONCAT(
-> GROUP_CONCAT(
-> CONCAT(`question_type_id`, ', ', `answer`)
-> SEPARATOR ', ' ), ', ', `created_at`) AS RESULT
-> FROM answer
-> -- WHERE `assessment_id` = 1
-> GROUP BY `user_id`
-> ORDER BY `user_id`;
+---------+---------------------+---------------------------------------------+
| user_id | assessment_name | RESULT |
+---------+---------------------+---------------------------------------------+
| 11 | Three Intelligences | 6, 4, 4, 4, 6, 5, 2016-01-01 00:00:00 |
| 12 | Three Intelligences | 6, 4, 6, 4, 6, 4, 6, 4, 2016-01-01 00:00:00 |
+---------+---------------------+---------------------------------------------+
2 rows in set (0.00 sec)
MariaDB []>
请告诉我它是否适合您