Ich habe eine Tabelle wie folgt aus:Wie erhält man den Wert verwandter Zeilen?
// QandA
+----+----------+----------------+------+---------+-----------+
| id | subject | acceptedanswer | type | related | id_author |
+----+----------+----------------+------+---------+-----------+
| 1 | subject1 | NULL | 0 | NULL | 123 |
| 2 | | 1 | 1 | 1 | 452 |
| 3 | subject2 | NULL | 0 | NULL | 635 |
| 4 | | 1 | 1 | 3 | 432 |
| 5 | | NULL | 1 | 1 | 246 |
+----+----------+----------------+------+---------+-----------+
/* columns explanations:
- acceptedanswer can be NULL, 0, 1. (0 and NULL are identical). (it is NULL for questions)
- type is 0 for questions and 1 for answers.
- related is NULL for questions. For answers is containing the number of its own question
*/
Nun, ich habe zwei Parameter:
id
Nummer einer Antwort:$id = 5
id_author
dieser Antwort$id_author = 246
Jetzt Ich versuche, diese zwei Dinge zu bekommen:
- Gegenstand seiner verdanken Frage
- Spezifizierungs jede akzeptierte-Antwort oder keine eigene Frage hat.
Hier ist meine query:
SELECT t2.subject
FROM qanda t1
JOIN qanda t2 ON t1.related = t2.id AND t2.type = 0
WHERE t1.id = $id AND t2.id_author = $id_author
Aktuelles Ergebnis:
+----------+
| subject1 |
+----------+
Erwartetes Ergebnis:
+----------+---+
| subject1 | 1 |
+----------+---+
-- ^this 1 means the question of this answer has a accepted answer
-- ^this should be 0 if there isn't any accepted answer for that question
Wie kann ich das tun?
Das ist falsch ..! Wie ich in der Frage erwähnt habe, habe ich eine ID einer Antwort ... Sie haben '$ id' in Ihrer Anfrage überhaupt nicht verwendet. – stack
Eigentlich habe ich es herausgefunden .. Ich muss hinzufügen, dass where-Klausel: '. . . wo x.type = 0 und x.id = $ id' – stack
Sie wissen, Ihre Abfrage funktioniert auch, nur es ist seltsam für mich .. warum Ihre Abfrage funktioniert? Ich bin mir ziemlich sicher, dass ich zwei 'Joins' in diesem Fall brauche. aber deine arbeitet mit einem "Join". Kannst du bitte bitte einige Erklärungen zu deiner Antwort hinzufügen? Speziell erklären, was ist das "DISTINCT" und diese Note, die Ende Ihrer Antwort ist .. – stack