【问题标题】:MySQL: GROUP_CONCAT with LEFT JOINMySQL:带有 LEFT JOIN 的 GROUP_CONCAT
【发布时间】:2011-05-26 06:03:14
【问题描述】:

我遇到了 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.');

请注意,此帮助台数据库有两张票,每张票都有两个解决方案条目。我的目标是使用 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 的信息以及工单 1 和工单 2 的解决方案条目。

我做错了什么?谢谢!

【问题讨论】:

    标签: sql mysql join aggregate-functions group-concat


    【解决方案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
    

    【讨论】:

    • 但是为什么尼克的方法不起作用?显然不是,但似乎应该。你能给我解释一下吗?
    • 到目前为止,我理解 group_contact 不关心查询的部分,它对所有中间结果进行分组。
    • 我认为我的 joins 是错误的,但我只是错过了 GROUP BY 声明,所以 GROUP_CONCAT() 没有汇总值。希望这可以节省一些人的时间,就像我花在斗鸡眼上的时间一样。
    • 这里给出的“替代”答案对于更大的数据集性能更高
    • 替代答案看起来更干净。那是给我的。它有效!
    【解决方案2】:

    您只需要添加一个 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;
    

    【讨论】:

      【解决方案3】:

      我认为@Dylan Valade 的评论是最简单的答案,所以我将其作为另一个答案发布:只需将 GROUP BY Tickets.id 添加到 OP 的 SELECT 即可解决问题。它解决了我自己的问题。

      但是,对于不小的数据库,可接受的答案,特别是如果门票上有任何谓词。id 似乎不涉及全表扫描,因此虽然上一段返回正确的结果,但它的效率似乎要低得多就我而言。

      【讨论】:

        猜你喜欢
        • 2012-07-28
        • 2014-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        相关资源
        最近更新 更多