Reputation: 939
CONCAT
within GROUP_CONCAT
, what is wrong with following mysql code ? Please see SQL Fiddle, full code is there.
Let me explain, I have 5 table
cls - List of Classes
sec - List of Sections
fee - List of Fee
cls_sec - List of section assigned to each class
cls_fee - List of fee assigned to each section
Table cls
- lists of Class
id | ttl
===========
1 | One
2 | Two
3 | Three
Table sec
- lists of section
id | ttl
===========
1 | A
2 | B
Table cls_sec
- lists of each section assigned to Class
id | c_id| s_id
=====================
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
Table fee
- lists of fee category
id | ttl
===========
1 | Annual
2 | Monthly
3 | Library
Table cls_fee
- lists of each fee and amount assigned to Class
id | c_id| s_id| f_id| fee
=====================================
1 | 1 | 1 | 1 | 2000
2 | 1 | 1 | 2 | 500
3 | 1 | 2 | 1 | 3000
4 | 1 | 2 | 2 | 400
5 | 2 | 1 | 1 | 4500
6 | 2 | 1 | 2 | 450
7 | 3 | 0 | 1 | 5000
8 | 3 | 0 | 2 | 600
9 | 3 | 0 | 3 | 300
Here I am trying to include all relation in one GROUP_CONCAT
Result
My current output (Class name and section name is fetched repetitively according to fee )
//Class Name - Section Name (if exist) - fee, Class Name - Section Name (if exist) - fee ..
3.Three.Library->300, 3.Three.Monthly->600, 3.Three.Annual->5000,
2.Two-A.Monthly->450, 2.Two-A.Annual->4500, 1.One-A.Monthly->500,
1.One-A.Annual->2000, 1.One-B.Monthly->400, 1.One-B.Annual->3000
with following code
GROUP_CONCAT(DISTINCT CONCAT('\r\n',cls.id,'.',cls.ttl,
COALESCE(CONCAT('-',sec.ttl),''),COALESCE(CONCAT('.',fee.ttl,'->',cls_fee.fee)))
ORDER BY sec.id) AS cls
But what I want (remove duplication class and section)
//Class Name - Section Name (if exists) - fee, fee
3.Three.Library->300,Monthly->600,Annual->5000,
2.Two-A.Monthly->450,Annual->4500,
1.One-A.Monthly->500,Annual->2000,
1.One-B.Monthly->400,Annual->3000
So I add CONCAT
within nested CONCAT
GROUP_CONCAT(DISTINCT CONCAT('\r\n',cls.id,'.',cls.ttl,
COALESCE(CONCAT('-',sec.ttl,COALESCE(CONCAT('.',fee.ttl,'->',cls_fee.fee))), ''))
ORDER BY sec.id) AS cls
and got output, but it doesn't fetch as expected, also missing some fee
3.Three,
2.Two-A.Monthly->450, 2.Two-A.Annual->4500,
1.One-A.Monthly->500, 1.One-A.Annual->2000,
1.One-B.Monthly->400, 1.One-B.Annual->3000
MySQL CODE
SELECT
GROUP_CONCAT(DISTINCT CONCAT('\r\n',cls.id,'.',cls.ttl,
COALESCE(CONCAT('-',sec.ttl),''),COALESCE(CONCAT('.',fee.ttl,'->',cls_fee.fee)))
ORDER BY sec.id) AS cls
FROM
cls
LEFT JOIN
cls_sec ON cls_sec.cls = cls.id
LEFT JOIN
sec ON sec.id = cls_sec.sec
LEFT JOIN
cls_fee ON cls_fee.c_id = cls.id
LEFT JOIN
fee ON fee.id = cls_fee.f_id
WHERE
CASE WHEN cls_fee.s_id != 0 THEN cls_fee.s_id = sec.id ELSE cls.id END
Upvotes: 1
Views: 219
Reputation: 46219
You can try to use a subquery to write GROUP_CONCAT
from detail by cls.id, cls.ttl
then do GROUP_CONCAT
again in the main query.
Query 1:
SELECT GROUP_CONCAT(CONCAT(Id,'.',ttl,'.',flag,cls) ORDER BY Id desc,flag) result
FROM (
SELECT
cls.id,
cls.ttl,
COALESCE(CONCAT('-',sec.ttl),'') flag,
GROUP_CONCAT(DISTINCT CONCAT(
COALESCE(CONCAT('.',fee.ttl,'->',cls_fee.fee)))
ORDER BY sec.id) AS cls
FROM
cls
LEFT JOIN
cls_sec ON cls_sec.cls = cls.id
LEFT JOIN
sec ON sec.id = cls_sec.sec
LEFT JOIN
cls_fee ON cls_fee.c_id = cls.id
LEFT JOIN
fee ON fee.id = cls_fee.f_id
WHERE
CASE WHEN cls_fee.s_id != 0 THEN cls_fee.s_id = sec.id ELSE cls.id END
GROUP BY
cls.id,
cls.ttl,
COALESCE(CONCAT('-',sec.ttl),'')
)t1
| result |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 3.Three..Library->300,.Monthly->600,.Annual->5000,2.Two.-A.Monthly->450,.Annual->4500,1.One.-A.Monthly->500,.Annual->2000,1.One.-B.Monthly->400,.Annual->3000 |
Upvotes: 1