简体   繁体   中英

mysql select join 4 tables

Anyone can help me to make query as image below? Need your help guys.

I describe it by image. Hope you all can understand my problem.

My query i have done..

select A.ticket_id,A.number,B.subject,C.value as service,C.value as vendor,C.value as category,C.value as action
from tableA A
left join tableB B ON A.ticket_id = B.ticket_id
left join tableD C ON C.id = B.vendor
where A.ticket_id = 28 or A.ticket_id = 36 or A.ticket_id = 149;

the problem is service,vendor,category & action refer to same field = C.value

How to solved this issue?

加入4张桌子

Thanks

Run the below query and see if you get desired values

SELECT A.ticket_id,A.number,B.subject,GROUP_CONCAT(DISTINCT C3.value) as service,C.value as vendor,C1.value as category,C2.value as action
FROM tableA A
LEFT JOIN tableB B ON A.ticket_id = B.ticket_id
LEFT JOIN tableD C ON C.id = B.vendor
LEFT JOIN tableD C1 ON C1.id = B.category
LEFT JOIN tableD C2 ON C2.id = B.action
LEFT JOIN tableD C3 ON FIND_IN_SET(C3.id,B.service) > 0
WHERE A.ticket_id IN (28,36,149)
GROUP BY A.ticket_id

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM