MySQL 查询最新2天记录对比,按Group_Concat聚合函数统计

发布于 2017-03-02  56.08k 次阅读


所需的业务是个聚合统计,如
jacky     a
jacky     b
jacky     c查询出来的这种3条数据,怎么合并成一条?
[jacky       a,b,c]
见下图:
MySQL 查询最新2天记录对比,按Group_Concat聚合函数统计
MySQL 查询最新2天记录对比,按Group_Concat聚合函数统计
SQL:

select asin,GROUP_CONCAT(concat_ws(':',category_name,ranking) ORDER BY id asc SEPARATOR ',') from (
select t1.*,t1.rank-t2.rank as ranking from (
select
    * 
from
    bz_listing_rank b
where
    exists(
        select 1 from (select max(create_date) as create_date from bz_listing_rank) a
        where date(a.create_date)=date(b.create_date)
    )
) t1
left join (
select
    * 
from
    bz_listing_rank b
where
    exists(
        select 1 from (select subdate(max(create_date),interval 1 day) as create_date from bz_listing_rank) a
        where date(a.create_date)=date(b.create_date)
    )
) t2
on t1.asin= t2.asin
and t1.category_name = t2.category_name

)  tt  group by asin

 

1、使用语法及特点:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] [,col ...]]
[SEPARATOR str_val])
在 MySQL 中,你可以得到表达式结合体的连结值。通过使用 DISTINCT 可以排除重复值。如果希望对结果中的值进行排序,可以使用 ORDER BY 子句。
SEPARATOR 是一个字符串值,它被用于插入到结果值中。缺省为一个逗号 (","),可以通过指定 SEPARATOR "" 完全地移除这个分隔符。
可以通过变量 group_concat_max_len 设置一个最大的长度。在运行时执行的句法如下: SET [SESSION | GLOBAL] group_concat_max_len = unsigned_integer;
如果最大长度被设置,结果值被剪切到这个最大长度。如果分组的字符过长,可以对系统参数进行设置:SET @@global.group_concat_max_len=40000;

2、使用示例:
语句 SELECT locus,GROUP_CONCAT(id) FROM info WHERE locus IN('AB086827','AF040764') GROUP BY locus; 的返回结果为
+----------+------------------+
| locus    | GROUP_CONCAT(id) |
+----------+------------------+
| AB086827 | 1,2              |
| AF040764 | 23,24            |
+----------+------------------+

语句 SELECT locus,GROUP_CONCAT(distinct id ORDER BY id DESC SEPARATOR '_') FROM info WHERE locus IN('AB086827','AF040764') GROUP BY locus;的返回结果为
+----------+----------------------------------------------------------+
| locus    | GROUP_CONCAT(distinct id ORDER BY id DESC SEPARATOR '_') |
+----------+----------------------------------------------------------+
| AB086827 | 2_1                                                      |
| AF040764 | 24_23                                                    |
+----------+----------------------------------------------------------+

语句SELECT locus,GROUP_CONCAT(concat_ws(', ',id,journal) ORDER BY id DESC SEPARATOR '. ') FROM info WHERE locus IN('AB086827','AF040764') GROUP BY locus;的返回结果为
+----------+--------------------------------------------------------------------------+
| locus    | GROUP_CONCAT(concat_ws(', ',id,journal) ORDER BY id DESC SEPARATOR '. ') |
+----------+--------------------------------------------------------------------------+
| AB086827 | 2, Submitted (20-JUN-2002). 1, Unpublished                               |
| AF040764 | 24, Submitted (31-DEC-1997) . 23, Unpublished                            |
+----------+--------------------------------------------------------------------------+

附件:SQL