我在MSQL中有三个表,如下所示:
customer_details:
cust_id cust_name Mob_No
-----------------------------------------
1 john 9999999999
2 Rex 1234567890
customer_services:
cust_id service_id
--------------------------
1 s1
2 s2
1 s2
m_service:
service_id service_name
----------------------------
s1 IT
s2 VAT
我需要如下结果:
cust_id cust_name service_name
--------------------------------------------
1 John IT, VAT
2 Rex VAT
我试过如下:
SELECT a.cust_id,a.cust_name, c.service_name
FROM customer_details a, customer_service b, m_service c
WHERE a.cust_id=b.cust_id AND b.service_id=c.service_id;
但我需要同一行中的服务名称。任何帮助都非常值得关注
谢谢和问候 Biswa
答案 0 :(得分:0)
您可以使用MySQL的GROUP_CONCAT()函数生成输出中所需的服务名称的CSV列表:
SELECT
t1.cust_id,
t1.cust_name,
GROUP_CONCAT(t3.service_name) AS service_name
FROM customer_details t1
INNER JOIN customer_services t2
ON t1.cust_id = t2.cust_id
INNER JOIN m_service t3
ON t2.service_id = t3.service_id
GROUP BY
t1.cust_id,
t1.cust_name
在这里演示: