GROUP_CONCAT中的CONCAT-如何删除重复的结果

时间:2018-11-03 13:06:31

标签: mysql sql group-concat

CONCAT中的

GROUP_CONCAT,下面的mysql代码有什么问题?请参阅SQL Fiddle,其中有完整的代码。

让我解释一下,我有5张桌子

cls -类列表

-部分列表

费用-费用列表

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

cls_fee -分配给每个部分的费用列表

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 Fiddle

1 个答案:

答案 0 :(得分:1)

您可以尝试使用子查询以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

Results

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