concat在组中\u concat-如何删除重复结果

vom3gejh  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(247)

CONCATGROUP_CONCAT ,下面的mysql代码有什么问题?请看sql fiddle,完整的代码在那里。
让我解释一下,我有5张table
cls-类列表
章节列表
费用-费用清单
cls\ U sec-分配给每个类的节的列表
cls\ U费用-分配给每个部分的费用清单
cls -班级名单

id  |   ttl
===========
1   |   One
2   |   Two
3   |   Three

sec -章节列表

id  |   ttl
===========
1   |   A
2   |   B

cls_sec -分配给班级的每个部分的列表

id  |   c_id|   s_id    
=====================
1   |   1   |   1
2   |   1   |   2
3   |   2   |   1

fee -费用类别列表

id  |   ttl
===========
1   |   Annual
2   |   Monthly
3   |   Library

cls_fee -分配给班级的每项费用和金额的清单

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

在这里,我试图把所有的关系都包含在一起 GROUP_CONCAT 结果
我的当前输出(类名和节名根据费用重复获取)

//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

具有以下代码

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

但我想要的是(删除重复类和节)

//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

所以我补充说 CONCAT 嵌套内 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

并且得到了输出,但是它没有得到预期的效果,还缺少一些费用

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代码

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

sql小提琴
o2gm4chl

o2gm4chl1#

您可以尝试使用子查询来编写 GROUP_CONCAT 根据细节 cls.id, cls.ttl 那就做吧 GROUP_CONCAT 再次在主查询中。
查询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 |

相关问题