MySQL: GROUP CONCAT with LEFT JOIN

Mam problem z funkcją "GROUP_CONCAT" w MySQL. Zilustruję mój problem za pomocą prostej bazy help desk:

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.');

Zauważ, że ta baza danych help desk ma dwa bilety, każdy z dwoma wpisami rozwiązania. Moim celem jest użycie instrukcji SELECT, aby utworzyć listę wszystkich biletów w bazie danych z ich wpisami rozwiązania corrosponding. To jest instrukcja SELECT, której używam:

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

Problem z powyższą instrukcją SELECT polega na tym, że zwraca tylko jeden wiersz:

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.

Zauważ, że zwraca informacje o bilecie 1 wraz z biletem 1 i biletem 2.

Co robię źle? Dzięki!
Author: Nick, 2010-12-16

2 answers

Użycie:

   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

Alternatywny:

   SELECT t.*,
          (SELECT GROUP_CONCAT(s.soution)
             FROM SOLUTIONS s 
            WHERE s.ticket_id = t.ticket_id) AS combinedsolutions
     FROM TICKETS t
 77
Author: OMG Ponies,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-12-15 23:24:00

Myślę ,że komentarz @ Dylan Valade jest najprostszą odpowiedzią, więc zamieszczam go jako inną odpowiedź: po prostu dodanie grupy przez Tickets.id aby wybrać OP powinien rozwiązać problem. To naprawiło mój własny problem.

Jednak dla baz danych, które nie są małe akceptowana odpowiedź, zwłaszcza jeśli istnieją jakieś predykaty na Tickets.id wydaje się, że nie obejmuje całkowitego skanowania tabeli, a więc podczas gdy poprzedni akapit zwraca prawidłowe wyniki, wydaje się być znacznie mniej wydajny w moim przypadku.

 1
Author: ghr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-04 05:35:43