栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在整个子查询上使用group_concat?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在整个子查询上使用group_concat?

解决方案就是完全省略

group by 1 = 1
。我假设这
group_concat
将要求我为其提供一个组,但是可以将其直接用于子查询,如下所示:

select group_concat(id,col1,col2) from    (select * from some_table     where id >= 2 and id < 5     order by id desc) as some_table;

请注意,需要将null值强制转换为concat-friendly,例如:

insert into some_table (col1, col2)     values ('a', 1), ('b', 11), ('c', NULL), ('d', 25), ('e', 50);select group_concat(id, col1, col2) from    (select id, col1, ifnull(col2, 'NULL') as col2     from some_table     where id >= 2 and id < 5     order by id desc) as some_table;

输出:

+------------------------------+| group_concat(id, col1, col2) |+------------------------------+| 2b11,3cNULL,4d25  |+------------------------------+

另一个警告:mysql的最大长度

group_concat
由变量:定义
group_concat_max_len
。为了散列 n个
表行的串联,我需要:

  1. 哈希行,使其以32位表示,而不管其具有多少列
  2. 确保
    group_concat_max_len > (n * 33)
    (多余的字节用于添加逗号)
  3. group_concat
    列哈希行。

最终,我最终使用了客户端语言来检查每列的名称,编号和可空性,然后构建如下查询:

select md5(group_concat(row_fingerprint)) from    (select concat(id, col1, ifnull(col2, 'null')) as row_fingerprint     from some_table     where id >= 2 and id < 5     order by id desc) as foo;

有关更多详细信息,您可以在此处浏览我的代码(请参见函数:find_diff_intervals)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626583.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号