2016-04-20 6 views
0

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 
+0

ich SQLFiddle versucht, mit, aber es ist nicht richtig funktioniert jetzt. – Hobbyist

+0

Verwenden Sie keine Komma-Join-Syntax. Siehe JOINs – Strawberry

+0

Und alle Antworten sind "0" (d. H. Falsch) – Strawberry

Antwort

0

Sie verwenden GROUP BY auf question_id. Dadurch wird es so, dass es nur eine Zeile für jede Frage zeigt, haben Sie diese Zeile in Ihrer Anfrage Sie haben die folgende Zeile in der Abfrage

auch zu entfernen:

AND `test_answers`.`answer_id` = `test_questions`.`question_id` 

Dies scheint nicht richtig zu mir? Dies sollte die Frage nach der Frage sein

ich die Tabelle in meiner eigenen Datenbank neu erstellen (sqlfiddle Erfahrungen Timeouts):

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` 

Die ANSWER_TEXT von answer_id = 5 ist zwar falsch, aber ich denke, das ist nur ein Tippfehler in den Beispieldaten.

0

Das erste Problem ist Ihre GROUP BY, die nicht benötigt wird. Sie würden es brauchen, zum Beispiel, wenn Sie die Anzahl der guten Antwort pro Frage

zählen wollten Das zweite Problem, das Sie haben, ist ORDER BY, wenn Sie wirklich die Ausgabe wollen, wie Sie es sagen. Sie müssen auf den zweiten Parameter answer_id für hinzufügen

Wie Strawberry erwähnt, es ist besser für die Lesbarkeit, wenn Sie JOIN verwenden Syntax (explizite) anstelle von mehreren FROM (implizite)

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 
0

Das ist genau das, was du bist suche:

SELECT CONCAT('question_id: ',Q.`question_id`) AS QID, 
     CONCAT('answer_id: ', A.answer_id) AS AID, 
     CONCAT('Text: ', A.answer_text) AS Atxt 
FROM `user_tests` T 
INNER JOIN `test_questions` Q ON Q.`question_belongs_to` = T.`test_id` 
INNER JOIN `test_answers` A ON A.`answer_belongs_to` = Q.`question_id` 
WHERE T.`test_owner` = 1 #The "user id" 
ORDER BY Q.`question_id` 

|   QID |   AID |      Atxt | 
|----------------|--------------|-----------------------------| 
| 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 1 | 
| 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 | 

SQL Fiddle

Sie haben eine GROUP BY verwenden und Sie brauchen nicht, dass, wenn Sie wollen ein ll Antworten.

Zusätzlich verwendete man implizite Join-Syntax, die eine schlechte Gewohnheit, immer explizit JOIN-Syntax verwendet

Verwandte Themen