求一mysql函数 返回一个号码的所有组合

hi_kevin 2012-09-04 03:47:37
如题:例如传入1234 则返回24种组合 即:1234,1243,2134,2341……
传入1123返回12种:1123,1132,3121……
传入1122 返回6种: 1122,1212,1221,2211,2121,2112
传入1112 返回4种: 1112,1121,1211,2111

...全文
108 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hi_kevin 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

select distinct concat(mid('例如传入',t1.x,1) ,mid('例如传入',t2.x,1) ,mid('例如传入',t3.x,1) ,mid('例如传入',t4.x,1) )
from (
select 1 as x union all
select 2 union all
select 3 union all
select 4
) t1,
(
……
[/Quote]

也不是正确的结果哦 可以试下1113应该返回4个结果,1234应该返回24个结果 才是正确的哦
ACMAIN_CHM 2012-09-04
  • 打赏
  • 举报
回复
select distinct concat(mid('例如传入',t1.x,1) ,mid('例如传入',t2.x,1) ,mid('例如传入',t3.x,1) ,mid('例如传入',t4.x,1) )
from (
select 1 as x union all
select 2 union all
select 3 union all
select 4
) t1,
(
select 1 as x union all
select 2 union all
select 3 union all
select 4
) t3,
(
select 1 as x union all
select 2 union all
select 3 union all
select 4
) t3,
(
select 1 as x union all
select 2 union all
select 3 union all
select 4
) t4
hi_kevin 2012-09-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

把每个数字挨个插入一个表中tmp

然后

select distinct concat(A.num,B.num,C.num,D.num)
from temp A,temp B,temp C ,temp D
[/Quote]

不对哦 如1113 存入会出现 1111,3333等 而正确的应该只有四个:1113,1131,1311,3111
rucypli 2012-09-04
  • 打赏
  • 举报
回复
把每个数字挨个插入一个表中tmp

然后

select distinct concat(A.num,B.num,C.num,D.num)
from temp A,temp B,temp C ,temp D
dota89 2012-09-04
  • 打赏
  • 举报
回复
这种不就是简单的数学上的排列吗,算法都有了,用sql写出来,很简单的

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧