2010-12-15 12 views
37

Ich habe ein Problem mit der MySQL "GROUP_CONCAT" -Funktion. Ich werde mein Problem mit einem einfachen Help Desk-Datenbank illustrieren:MySQL: GROUP_CONCAT mit LINKEN JOIN

CREATE TABLE Tickets (
id INTEGER NOT NULL PRIMARY KEY, 
requester_name VARCHAR(255) NOT NULL, 
description TEXT NOT NULL); 

CREATE TABLE Solutions (
id INTEGER NOT NULL PRIMARY KEY, 
ticket_id INTEGER NOT NULL, 
technician_name VARCHAR(255) NOT NULL, 
solution TEXT NOT NULL, 
FOREIGN KEY (ticket_id) REFERENCES Tickets.id); 

INSERT INTO Tickets VALUES(1, 'John Doe', 'My computer is not booting.'); 
INSERT INTO Tickets VALUES(2, 'Jane Doe', 'My browser keeps crashing.'); 
INSERT INTO Solutions VALUES(1, 1, 'Technician A', 'I tried to solve this but was unable to. I will pass this on to Technician B since he is more experienced than I am.'); 
INSERT INTO Solutions VALUES(2, 1, 'Technician B', 'I reseated the RAM and that fixed the problem.'); 
INSERT INTO Solutions VALUES(3, 2, 'Technician A', 'I was unable to figure this out. I will again pass this on to Technician B.'); 
INSERT INTO Solutions VALUES(4, 2, 'Technician B', 'I re-installed the browser and that fixed the problem.'); 

Beachten Sie, dass diese Help-Desk-Datenbank zwei Karten hat, die jeweils mit zwei Lösungseinträge. Mein Ziel ist es, mit einer SELECT-Anweisung eine Liste aller Tickets in der Datenbank mit ihren entsprechenden Lösungseinträgen zu erstellen. Dies ist die SELECT-Anweisung Ich verwende:

SELECT Tickets.*, GROUP_CONCAT(Solutions.solution) AS CombinedSolutions 
FROM Tickets 
LEFT JOIN Solutions ON Tickets.id = Solutions.ticket_id 
ORDER BY Tickets.id; 

Das Problem mit der obigen SELECT-Anweisung wird es zurückkehrt nur eine Zeile:

id: 1 
requester_name: John Doe 
description: My computer is not booting. 
CombinedSolutions: I tried to solve this but was unable to. I will pass this on to Technician B since he is more experienced than I am.,I reseated the RAM and that fixed the problem.,I was unable to figure this out. I will again pass this on to Technician B.,I re-installed the browser and that fixed the problem. 

Beachten Sie, dass es beim Ticket 1 der Informationen sowohl mit Ticket 1'en ist Rückkehr und Die Lösungseinträge von Ticket 2.

Was mache ich falsch? Vielen Dank!

Antwort

71

Verwendung:

SELECT t.*, 
      x.combinedsolutions 
    FROM TICKETS t 
LEFT JOIN (SELECT s.ticket_id, 
        GROUP_CONCAT(s.soution) AS combinedsolutions 
      FROM SOLUTIONS s 
     GROUP BY s.ticket_id) x ON x.ticket_id = t.ticket_id 

Alternative:

SELECT t.*, 
      (SELECT GROUP_CONCAT(s.soution) 
      FROM SOLUTIONS s 
      WHERE s.ticket_id = t.ticket_id) AS combinedsolutions 
    FROM TICKETS t 
+1

Das funktioniert! Vielen Dank! – Nick

+3

Aber warum funktioniert Nick's Methode nicht? Offensichtlich nicht, aber es scheint so, als müsste es. Kannst du es mir erklären? – pbarney

+0

Soweit ich es verstehe, group_contact kümmert sich nicht um die Teile sof die Abfrage, es gruppiert alle Zwischenergebnisse. – jnovacho

1

denke ich @Dylan Valade Kommentar die einfachste Antwort ist, damit ich es als eine andere Antwort bin Entsendung: einfach eine GROUP BY Tickets.id Hinzufügen zu dem SELECT des OP sollte das Problem beheben. Es hat mein eigenes Problem behoben.

Aber für Datenbanken, die nicht klein sind, scheint die akzeptierte Antwort, insbesondere wenn es irgendwelche Prädikate für Tickets.id gibt, keinen gesamten Tabellenscan zu enthalten, und obwohl der vorherige Absatz die korrekten Ergebnisse zurückgibt, scheint er viel weniger zu sein effizient in meinem Fall.