Chinaunix

标题: 跪求一SQL [打印本页]

作者: yang_java2004    时间: 2009-03-25 09:56
标题: 跪求一SQL
有如下数据:

00000
00001
00002
00003
00004

.........

99998
99999


求匹配如下模式的数据:ABABA,AAABB,ABCDE  ,其中ABCDE,为0~9的数字

这样的sql怎么写?
作者: yang_java2004    时间: 2009-03-25 09:57
标题: 回复 #1 yang_java2004 的帖子
在线等...
作者: ora-ora-ora    时间: 2009-03-25 11:03
没看懂
作者: welcome008    时间: 2009-03-25 11:48
原帖由 yang_java2004 于 2009-3-25 09:56 发表
有如下数据:

00000
00001
00002
00003
00004

.........

99998
99999


求匹配如下模式的数据:ABABA,AAABB,ABCDE  ,其中ABCDE,为0~9的数字

这样的sql怎么写?



我有一个超级笨的办法:
where (substr(field,1,1)=substr(field,3,1) and substr(field,1,1)=substr(field,5,1) and substr(field,2,1)=substr(field,4,1))
or
(......)
or
(......)


作者: yang_java2004    时间: 2009-03-25 12:03
这个办法太土了,那个规则我只列出了3个,其实还有30多个呢
作者: doni    时间: 2009-03-25 13:34
mod(
((ascii(substr(field,1,1))-ascii(substr(model,1,1)))*10000) +
((ascii(substr(field,2,1))-ascii(substr(model,2,1)))*1000) +
((ascii(substr(field,3,1))-ascii(substr(model,3,1)))*100) +
((ascii(substr(field,4,1))-ascii(substr(model,4,1)))*10) +
((ascii(substr(field,5,1))-ascii(substr(model,5,1)))*1),
11111) = 0
作者: welcome008    时间: 2009-03-25 13:36
想不出更好的办法了

想简化一点的话,那就先给那个表另加5个字段
然后分别截取相应位置的数据放进这5个字段里

最后再在where条件里对5个字段分别进行比较,可以少写一些substr
作者: yang_java2004    时间: 2009-03-25 15:17
原帖由 doni 于 2009-3-25 13:34 发表
mod(
((ascii(substr(field,1,1))-ascii(substr(model,1,1)))*10000) +
((ascii(substr(field,2,1))-ascii(substr(model,2,1)))*1000) +
((ascii(substr(field,3,1))-ascii(substr(model,3,1)))*100) +
((a ...




能解释下不?
作者: doni    时间: 2009-03-25 17:20
原帖由 doni 于 2009-3-25 13:34 发表
mod(
((ascii(substr(field,1,1))-ascii(substr(model,1,1)))*10000) +
((ascii(substr(field,2,1))-ascii(substr(model,2,1)))*1000) +
((ascii(substr(field,3,1))-ascii(substr(model,3,1)))*100) +
((a ...


想了一下,这个还是不对

这个直接用SQL不太好写,需要一些编程手段

[ 本帖最后由 doni 于 2009-3-25 17:21 编辑 ]
作者: dtsjs    时间: 2009-03-26 11:23
先建立一个模式表 create table hm_mod (hm_mod varchar2(5));
存 AAABB ABABA
再建一个表  create table hm (hm varchar2(5))
  存号码
SELECT *   FROM hm A ,HM_MOD B WHERE
TRANSLATE(HM_MOD,HM_MOD,hm) = hm;
这种用来试试看
作者: zolwjz    时间: 2009-03-26 12:13
好问题,关注中---
作者: doni    时间: 2009-03-26 12:28
原帖由 zolwjz 于 2009-3-26 12:13 发表
好问题,关注中---


但是,这个问题也许更适合用c,java,perl,php来解决,至少用一下PL/SQL,只用SQL有一定难度
作者: welcome008    时间: 2009-03-26 12:46
原帖由 dtsjs 于 2009-3-26 11:23 发表
先建立一个模式表 create table hm_mod (hm_mod varchar2(5));
存 AAABB ABABA
再建一个表  create table hm (hm varchar2(5))
  存号码
SELECT *   FROM hm A ,HM_MOD B WHERE
TRANSLATE(HM_MOD,HM_MOD, ...

这样的话,要把所有LZ需要的组合放进去
比如AAABB,那估计要把
11122
11133
...
99988
都放进去吧?
作者: doni    时间: 2009-03-26 13:39
原帖由 welcome008 于 2009-3-26 12:46 发表

这样的话,要把所有LZ需要的组合放进去
比如AAABB,那估计要把
11122
11133
...
99988
都放进去吧?


这个是难点




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2