开发者

How to concatenate the output of a SQL query into one string

I am newbie in writing SQL queries and this is for a mySQL database.

I have a Table called PatientVisit (PV), which has a one to one with BillMaster (BM). Each visit has one bill, which then has a one to many with BillDetail (BD). When i list out the Visit details from PatientVisit (PV), i need to print a string with the set of 'ServiceName' columns associated with that one visit.

So for example, the PatientVisit.ID number '1' has a corresponding BillMaster.Bill No '1' which has 2 entries in BillDetail 'Consultation' and 'Dressing'.

When i print details of Visit 1, i need 'Consultation,Dressing' as one string value for the 'Service Name' column.

If i had a one to one , then the query would have been simple as follow :

select PV.ID, BM.BillNo,  BD.ServiceName 
FROM PatientVisits PV INNER JOIN BillMaster BM ON BM.VisitID = PV.ID 
INNER JOIN BillDetail BD ON BD.BillNo = 开发者_高级运维BM.BillNo
WHERE ....

However, since it is one to many for the ServiceName column, how can this query be written ?

Thanks, Chak.


Try this

select PV.ID, BM.BillNo, 
    GROUP_CONCAT(BD.ServiceName)
    FROM PatientVisits PV INNER JOIN BillMaster BM ON BM.VisitID = PV.ID 
    INNER JOIN BillDetail BD ON BD.BillNo = BM.BillNo
    WHERE ..
    GROUP BY PV.ID,BM.BillNo

..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜