postgres group_concat ejemplos sql mysql join aggregate-functions group-concat

ejemplos - mysql group_concat order



MySQL: GROUP_CONCAT con LEFT JOIN (2)

Creo que el comentario de @Dylan Valade es la respuesta más simple, así que lo publico como otra respuesta: simplemente agregando un GROUP BY Tickets.id al SEL de OP debería solucionar el problema. Solucionó mi propio problema.

Sin embargo, para las bases de datos que no son pequeñas, la respuesta aceptada, especialmente si hay algún predicado en Tickets.id, parece no implicar un escaneo total de la tabla y, por tanto, aunque el párrafo anterior arroja los resultados correctos, parece ser mucho menos eficiente en mi caso .

Tengo un problema con la función "GROUP_CONCAT" de MySQL. Ilustraré mi problema usando una base de datos de mesa de ayuda simple:

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

Tenga en cuenta que esta base de datos de la mesa de ayuda tiene dos tickets, cada uno con dos entradas de solución. Mi objetivo es usar una declaración SELECT para crear una lista de todos los tickets en la base de datos con sus entradas de solución corrosponding. Esta es la declaración SELECT que estoy usando:

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

El problema con la declaración SELECT anterior es que solo devuelve una fila:

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.

Tenga en cuenta que devuelve la información del ticket 1 con las entradas de la solución del ticket 1 y del ticket 2.

¿Qué estoy haciendo mal? ¡Gracias!


Utilizar:

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

Alterno:

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