如何在没有分组依据的情况下使用 group_concat

how to use group_concat without group by

我有一个 table 如下所示

    +------------+----------------+--------------------------------+    
    | 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,但由于 table 没有任何其他唯一标识符,我无法使用分组依据。

结果应该类似于

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

这个呢。使用变量来决定哪个先

Sql Fiddle Demo

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

也许你需要检查

ORDER BY file_fields_id
to 
ORDER BY file_fields_id, value