如何在没有group by的情况下使用group_concat

时间:2015-12-03 16:21:34

标签: mysql group-by group-concat

我有一张如下表

    +------------+----------------+--------------------------------+    
    | date       | file_fields_id | value                          |  
    +------------+----------------+--------------------------------+  
    | 2015-12-03 |            124 | AAA                            |  
    | 2015-12-03 |            125 | BBB                            |  
    | 2015-12-03 |            126 | CCC                            |  
    | 2015-12-03 |            134 | T                              |  
    | 2015-12-03 |            135 | 22222333                       |  
    | 2015-12-03 |            136 | 5216                           |  
    | 2015-12-03 |            138 | D7989878978979892              |  
    | 2015-12-03 |            139 |                                |  
    | 2015-12-03 |            143 |                                |  
    | 2015-12-03 |            124 | AAA                            |   
    | 2015-12-03 |            125 | SDOGX                          |  
    | 2015-12-03 |            126 | CUSNETDOGSUSDEW--P-US-L--      |  
    | 2015-12-03 |            134 | MO                             |  
    | 2015-12-03 |            135 | 33333222                       |  
    | 2015-12-03 |            136 | 5128                           |  
    | 2015-12-03 |            138 | D54565210545542000             |  
    | 2015-12-03 |            139 |                                |  
    | 2015-12-03 |            143 |                                |  

我希望这两行中的数据为file_fields_id,每行124 - 143。我正在使用group_concat,但因为该表没有任何其他唯一标识符,所以我无法使用group by。

结果应该类似于

    | 2015-12-03 | ([124#AAA], [125#BBB], [126#CCC]... [138#D7989878978979892],[143#])|        
    | 2015-12-03 | ([124#AAA], [125#BBB], [126#CCC]... [138#D7989878978979892],[143#])|

1 个答案:

答案 0 :(得分:1)

这个怎么样?使用变量来决定哪个是第一个

<强> Sql Fiddle Demo

create

<强>输出

SELECT row_id, 
       date,
       group_concat( value ORDER BY value SEPARATOR ', ')
FROM 
    (SELECT 
        @row_number:=CASE
                        WHEN @customer_no = file_fields_id THEN @row_number + 1
                        ELSE 1
                     END AS row_id,
        @customer_no:= file_fields_id as file_fields_id,
        date,
        CONCAT('[',file_fields_id,'#',value,']') as value
    FROM
        Table1
     ORDER BY file_fields_id
     ) T
GROUP BY row_id, date
ORDER BY row_id;

也许你需要检查

| row_id |                       date |                                                                                      group_concat( value ORDER BY value SEPARATOR ', ') |
|--------|----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
|      1 | December, 03 2015 00:00:00 | [124#AAA], [125#SDOGX], [126#CUSNETDOGSUSDEW--P-US-L--], [134#MO], [135#22222333], [136#5128], [138#D54565210545542000], [139#], [143#] |
|      2 | December, 03 2015 00:00:00 |                           [124#AAA], [125#BBB], [126#CCC], [134#T], [135#33333222], [136#5216], [138#D7989878978979892], [139#], [143#] |