복붙노트

[SQL] MySQL의 : LEFT와 GROUP_CONCAT는 가입

SQL

MySQL의 : LEFT와 GROUP_CONCAT는 가입

나는 MySQL의의 "GROUP_CONCAT"기능에 문제가 발생하고있다. 나는 간단한 헬프 데스크 데이터베이스를 사용하여 내 문제를 설명합니다 :

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

이 헬프 데스크 데이터베이스가 두 솔루션 항목이 두 티켓, 각을 가지고주의. 내 목표는 자신의 corrosponding 솔루션 항목이 데이터베이스에 티켓의 모든 목록을 생성하는 SELECT 문을 사용하는 것입니다. 이것은 내가 사용하고 SELECT 문입니다 :

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

SELECT 문 위의 그것이 하나 개의 행을 반환하는 것입니다의 문제 :

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.

이 두 표 1과 표 2의 솔루션 항목으로 표 1의 정보를 반환 것 알 수 있습니다.

내가 무엇을 잘못하고 있지? 감사!

해결법

  1. ==============================

    1.사용하다:

    사용하다:

       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
    

    번갈아 하는:

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

    2.간단하게 문제를 해결해야 OP의 선택에 Tickets.id에 의해 그룹 추가 : 나는 @Dylan Valade의 코멘트 나는 다른 답변으로 게시하도록하겠습니다 있도록 간단한 대답라고 생각합니다. 그것은 내 자신의 문제를 해결.

    간단하게 문제를 해결해야 OP의 선택에 Tickets.id에 의해 그룹 추가 : 나는 @Dylan Valade의 코멘트 나는 다른 답변으로 게시하도록하겠습니다 있도록 간단한 대답라고 생각합니다. 그것은 내 자신의 문제를 해결.

    그러나 Tickets.id은 전체 테이블 스캔을 포함하지 나타나고에 어떤 술어가 특히, 허용 대답 작은 수없는 데이터베이스에 대한 그래서 이전 단락 반환하면서 나타나는 정확한 결과를 내 경우에는 훨씬 효율적으로 .

  3. ==============================

    3.넌 그냥 GROUP_BY을 추가해야합니다 :

    넌 그냥 GROUP_BY을 추가해야합니다 :

    SELECT Tickets.*, GROUP_CONCAT(Solutions.solution) AS CombinedSolutions FROM Tickets 
    LEFT JOIN Solutions ON Tickets.id = Solutions.ticket_id 
    GROUP_BY Tickets.id 
    ORDER BY Tickets.id;
    
  4. from https://stackoverflow.com/questions/4455958/mysql-group-concat-with-left-join by cc-by-sa and MIT license