MySQL从未成为我的强项。我创建了一个在线应用程序,允许用户为其他人创建测试,只是为了查看他们是否通过。这些测试的数据库结构如下:
user_test
- id (integer, primary key, auto increment)
- owner (integer)
user_test_question
- id (integer, primary key, auto increment)
- belongs_to_test (integer)
- question_text (varchar)
user_test_answer
- id (integer, primary key, auto increment)
- belongs_to_question (integer)
- answer_text (integer)
- answer_correct (integer)
这是我为测试场景创建的架构
CREATE TABLE users (
user_id INT(11) PRIMARY KEY AUTO_INCREMENT
);
CREATE TABLE user_tests (
test_id INT(11) PRIMARY KEY AUTO_INCREMENT,
test_owner INT(11) NOT NULL
);
CREATE TABLE test_questions (
question_id INT(11) PRIMARY KEY AUTO_INCREMENT,
question_belongs_to INT(11) NOT NULL,
question_text VARCHAR(200) NOT NULL
);
CREATE TABLE test_answers (
answer_id INT(11) PRIMARY KEY AUTO_INCREMENT,
answer_belongs_to INT(11) NOT NULL,
answer_text VARCHAR(200) NOT NULL,
answer_correct SMALLINT(1) DEFAULT 0
);
INSERT INTO users (user_id) VALUES (NULL);
INSERT INTO user_tests(test_id, test_owner) VALUES (NULL, 1);
INSERT INTO test_questions(question_id, question_belongs_to, question_text) VALUES (NULL, 1, "first question");
INSERT INTO test_questions(question_id, question_belongs_to, question_text) VALUES (NULL, 1, "second question");
INSERT INTO test_questions(question_id, question_belongs_to, question_text) VALUES (NULL, 1, "third question");
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 1, "Question 1 - answer 1", 0);
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 1, "Question 1 - answer 2", 0);
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 1, "Question 1 - answer 3", 0);
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 2, "Question 2 - answer 1", 0);
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 2, "Question 2 - answer 1", 0);
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 3, "Question 3 - answer 1", 0);
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 3, "Question 3 - answer 2", 0);
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 3, "Question 3 - answer 3", 0);
INSERT INTO test_answers(answer_id, answer_belongs_to, answer_text, answer_correct) VALUES (null, 3, "Question 3 - answer 4", 0);
以下是我尝试使用的查询:
SELECT * FROM `user_tests`, `test_questions`, `test_answers`
WHERE `user_tests`.`test_owner` = 1 #The "user id"
AND `test_answers`.`answer_id` = `test_questions`.`question_id`
GROUP BY `test_questions`.`question_id`
ORDER BY `test_questions`.`question_id`
结果只显示每个问题一个答案,预期结果如下:
按问题ID的顺序列出的答案,如下:
question_id: 1, answer_id: 1, text: Question 1 - Answer 1
question_id: 1, answer_id: 2, text: Question 1 - Answer 2
question_id: 1, answer_id: 3, text: Question 1 - Answer 3
question_id: 2, answer_id: 4, text: Question 2 - Answer 1
question_id: 2, answer_id: 5, text: Question 2 - Answer 2
question_id: 3, answer_id: 6, text: Question 3 - Answer 1
question_id: 3, answer_id: 7, text: Question 3 - Answer 2
question_id: 3, answer_id: 8, text: Question 3 - Answer 3
question_id: 3, answer_id: 9, text: Question 3 - Answer 4
答案 0 :(得分:0)
您在question_id上使用GROUP BY
。这将使每个问题只显示一行,您必须在查询中删除此行
此外,您在查询中还有以下行:
AND `test_answers`.`answer_id` = `test_questions`.`question_id`
这对我来说似乎不正确?这应该是belongs_to_question?|
我在自己的数据库中重建表(sqlfiddle体验超时):
SELECT * FROM `user_tests`, `test_questions`, `test_answers`
WHERE `user_tests`.`test_owner` = 1 #The "user id"
AND `test_answers`.`answer_belongs_to` = `test_questions`.`question_id`
ORDER BY `test_questions`.`question_id`, `test_answers`.`answer_id`
answer_id = 5的answer_text虽然不正确,但我想这只是样本数据中的一个拼写错误。
答案 1 :(得分:0)
第一个问题是您的GROUP BY不需要。你需要它,例如,如果你想计算好的答案数每个问题
如果您真的想要输出的话,那么您将遇到的第二个问题是ORDER BY。您需要为answer_id
添加第二个参数如草莓所述,如果使用JOIN语法(显式)而不是多个FROM(隐式),则可读性更好
SELECT question_id, answer_id, ...
FROM user_tests
INNER JOIN ON test_questions.question_belongs_to = user_tests.test_id
...
WHERE user_tests.test_owner = 1
答案 2 :(得分:0)
这正是您所寻找的:
var path = require('path');
function isBackgroundFile(file) {
// check file extension or something.
if(path.extname(file) === ".png" || path.extname(file) === ".jpg") {
return true;
}
return false;
}
function isNonBackgroundFile(file) {
return !isBackgroundFile(file);
}
您使用的是fs.readdir
,如果您想要所有答案,则不需要。
另外你使用隐式连接语法,这是一个坏习惯,总是使用显式JOIN语法