MySQL 数字辅助表应用一例 _MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2014 | 回复: 0   主题: MySQL 数字辅助表应用一例         下一篇 
tk
注册用户
等级:中士
经验:207
发帖:80
精华:0
注册:2011-10-19
状态:离线
发送短消息息给tk 加好友    发送短消息息给tk 发消息
发表于: IP:您无权察看 2015-8-14 9:54:54 | [全部帖] [楼主帖] 楼主

需求:一个字段有多行记录,查询结果为去重排序的一行记录,例如记录值为

 1,2,4

1,4,5

2,3

23,56,67

3,4

查询结果为:

1,2,3,4,5,23,56,67

    使用数字辅助表实现  

    -- 建立数字辅助表

    createtable nums (

    a intnotnullprimarykey

    );

    delimiter $$

    createprocedure pFastCreateNums(cnt int)

    begin

    declare s intdefault 1;

    truncatetable nums;

    insertinto nums select s;

    while s<=cnt do

    insertinto nums select a+s from nums where a+s <= cnt;

    set s=s*2;

    end while;

    commit;

    end $$

    delimiter ;

    call pFastCreateNums(1000000);

    -- 建立测试表

    createtable t1 (

    a varchar(100)

    );

    insertinto t1 values('1,2,4'),('1,4,5'),('2,3'),('23,56,67'),('3,4');

    commit;

    -- 查询

    select

    group_concat(a)

    from

    (select

    a

    from

    (select

    cast(substring_index(substring_index(t1.a, ',', nums.a), ',', - 1)

    as unsigned) a

    from

    t1, nums

    where

    nums.a <= length(t1.a) - length(replace(t1.a, ',', '')) + 1) t

    groupby a) t1;

    --转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论