3

大家好,我有一个小断言让我有点难过。我有一个如下表。(这是我的真实表的示例。我用它来解释,因为原始表有敏感数据。)

CREATE TABLE TEST01( 
TUID VARCHAR2(50),
FUND VARCHAR2(50),
ORG  VARCHAR2(50));
Insert into TEST01 (TUID,FUND,ORG) values ('9102416AB','1XXXXX','6XXXXX');
Insert into TEST01 (TUID,FUND,ORG) values ('9102416CC','100000','67130');
Insert into TEST01 (TUID,FUND,ORG) values ('955542224','1500XX','67150');
Insert into TEST01 (TUID,FUND,ORG) values ('915522211','1000XX','67XXX');
Insert into TEST01 (TUID,FUND,ORG) values ('566653456','xxxxxx','xxxxx');
Insert into TEST01 (TUID,FUND,ORG) values ('9148859fff','1XXXXXX','X6XXX');

插入后的表数据

"TUID"                        "FUND"                        "ORG"                         
"9102416AB"                   "1XXXXX"                      "6XXXXX"                      
"9102416CC"                   "100000"                      "67130"                       
"955542224"                   "1500XX"                      "67150"                       
"915522211"                   "1000XX"                      "67XXX"                       
"566653456"                   "xxxxxx"                      "xxxxx"                       
"9148859fff"                  "1XXXXXX"                     "X6XXX"   

“X”是通配符元素*(我继承了这个,我无法更改表格格式)*我想进行如下查询

select tuid from test01 where fund= '100000' and org= '67130'

但是我真正喜欢做的是检索任何包含这些段的记录,包括“X”

in other words the expected output here would be 

"TUID"                        "FUND"                        "ORG"                         
"9102416AB"                   "1XXXXX"                      "6XXXXX"                      
"9102416CC"                   "100000"                      "67130"                       
"915522211"                   "1000XX"                      "67XXX"                       
"566653456"                   "xxxxxx"                      "xxxxx"  

我已经开始编写一个庞大的 sql 语句,其中包含 12 个类似的语句,因为我必须比较组织并以各种可能的方式资助。这就是我要去的地方。但我想知道是否有更好的方法。

select * from test02
where fund = '100000' and org = '67130'
or fund like '1%' and org like '6%'
or fund like '1%' and org like '67%'
or fund like '1%' and org like '671%'
or fund like '1%' and org like '6713%'
or fund like '1%' and org like '67130'
or fund like '10%' and org like '6%'...etc

/*seems like there should be a better way..*/

谁能帮我想出这个sql语句...

顺便注意一下

 "9148859fff"                  "1XXXXXX"                     "X6XXX"   

被排除在预期结果之外,因为 org 中的第二个数字是“6”,我正在寻找任何看起来像“67130”的东西

4

3 回答 3

3

您可以使用该REPLACE()函数替换x为通配符_,然后使用LIKE

SELECT * 
FROM test01
WHERE '100000' LIKE REPLACE(REPLACE(fund, 'x', '_'), 'X', '_')
  AND  '67130' LIKE REPLACE(REPLACE(org,  'x', '_'), 'X', '_') ;

SQL-Fiddle测试

于 2013-05-14T16:52:16.870 回答
2

假设您要同时匹配“ X”和“ x”:

SELECT *
FROM Test01 t1
WHERE REGEXP_LIKE(t1.fund, '^[Xx1][Xx0][Xx0][Xx0][Xx0][X0x]$')  AND  
      REGEXP_LIKE(t1.org, '^[Xx6][Xx7][Xx1][Xx3][Xx0]$') 
;

我的正则表达式控制每个字段中的字符总数:6 infund5 in org

这是SQL Fiddle

于 2013-05-14T16:52:41.613 回答
0

这可能有效

从 test01 t 中选择 *   
    其中 ((t.fund like ('1%') 或 t.fund like ('X%'))   
    和 (t.org like ('6%') 或 t.org like ('X%'))   
    和 t.org not Like('%6%')   
于 2013-05-14T16:47:18.923 回答