MySQL war nie meine Stärke. Ich habe eine Online-Anwendung erstellt, mit der Benutzer Tests für andere Personen erstellen können, um festzustellen, ob sie erfolgreich sind oder nicht. Die Datenbankstruktur dieser Tests sind wie folgt:Versuchen, eine Abfrage zu machen, um alle Antworten auf alle Fragen zu einem Test zu erhalten
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)
Hier ist ein Schema I für ein Testszenario erstellt
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);
Hier ist die Abfrage, die ich versucht habe, mit:
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`
Die obwohl Ergebnisse gezeitigt zeigen nur eine Antwort pro Frage, und die erwarteten Ergebnisse wie so wäre:
Antworten in der Reihenfolge der Frage-ID, etwa so:
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
ich SQLFiddle versucht, mit, aber es ist nicht richtig funktioniert jetzt. – Hobbyist
Verwenden Sie keine Komma-Join-Syntax. Siehe JOINs – Strawberry
Und alle Antworten sind "0" (d. H. Falsch) – Strawberry