mysql树形left join_MySQL:具有LEFT JOIN的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.');

请注意,该帮助台数据库有两个票证,每个票证都有两个解决方案条目。我的目标是使用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的解决方案条目。

我究竟做错了什么?谢谢!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值