这是我的查询
SELECT dia
FROM CRES
WHERE pro_id = 2
AND 8103434563 LIKE
( SELECT dial_pattern||'%' FROM CDIVN WHERE dial_id = 1
);
现在select dial_pattern||'%' from CDIVN where dial_id = 1
可以提供multiple
个结果。因此,我的主要查询失败,原因是"sub query returns more than one row"
。这是因为我提到了like
。
但我的逻辑需要like
,因为我希望8103434563具有来自表CDIVN的模式匹配条件。
如何修改我的查询。请帮忙。
=======
CREATE TABLE CDIVN
( DIAL_PATTERN_ID NUMBER NOT NULL ENABLE,
DIAL_PATTERN VARCHAR2(30 BYTE) NOT NULL ENABLE,
OTHERS VARCHAR2(64 BYTE),
CONSTRAINT "CDIVN_PK" PRIMARY KEY ("DIAL_PATTERN_ID", "DIAL_PATTERN")
);
Insert into CDIVN (DIAL_PATTERN_ID,DIAL_PATTERN,OTHERS) values (1,'810','abc');
Insert into CDIVN (DIAL_PATTERN_ID,DIAL_PATTERN,OTHERS) values (1,'811','xyz');
Insert into CDIVN (DIAL_PATTERN_ID,DIAL_PATTERN,OTHERS) values (1,'812','aaa');
Insert into CDIVN (DIAL_PATTERN_ID,DIAL_PATTERN,OTHERS) values (5,'999','www');
Insert into CDIVN (DIAL_PATTERN_ID,DIAL_PATTERN,OTHERS) values (9,'333','ewe');
CREATE TABLE CRES
( PROFILE_ID NUMBER NOT NULL ENABLE,
PROFILE_NAME VARCHAR2(50 BYTE) NOT NULL ENABLE,
DIALLED_PATTERN VARCHAR2(15 BYTE),
CONSTRAINT "CRES_PK" PRIMARY KEY ("PROFILE_ID")
) ;
Insert into CRES (PROFILE_ID,PROFILE_NAME,DIALLED_PATTERN) values (1,'A','1');
Insert into CRES (PROFILE_ID,PROFILE_NAME,DIALLED_PATTERN) values (2,'B','5');
Insert into CRES (PROFILE_ID,PROFILE_NAME,DIALLED_PATTERN) values (3,'C','9');
我有其他来源的CRES-profile id和一个号码,分别为1和81034345。
现在select DIALLED_PATTERN from CRES where PROFILE_ID=1;
这会给我DIALLED_PATTERN为1。
现在select DIAL_PATTERN from CDIVN where DIALLED_PATTERN_ID = 1 ( DIALLED_PATTERN )
这将使DIAL_PATTERN为810,811,812。
现在,如果81034345与任何一个810%或811%或812%匹配。然后我需要810%作为我的答案。
答案 0 :(得分:1)
如果我可以这么说,我也对sql有一个 IN LIKE 功能的疯狂痴迷。要执行此操作,您可以将其作为JOIN
写在表CRES
上,而不是将子查询放在您的条件中。
我看到的方式是子查询
( SELECT dial_pattern||'%' FROM CDIVN WHERE dial_id = 1
);
条件中的不会直接影响表CRES
中的行,但会用作所有结果的过滤器。意思是,如果子查询没有返回(a)值,则整个查询也不应返回任何内容。
答案 1 :(得分:1)
收集like-values并构建一个regexp-expression,例如
-- Test Data
with CDIVN as
(select 1 as dial_id, 'ABC' as val
from dual
union all
select 1 as dial_id, 'ACD' as val
from dual
union all
select 1 as dial_id, 'XXA' as val from dual),
CRES as
(select 2 as proc_id, 'ABCD' as val
from dual
union all
select 2 as proc_id, 'DABCD' as val
from dual
union all
select 2 as proc_id, 'ACF' as val
from dual
union all
select 2 as proc_id, 'XXAF' as val from dual)
,
-- Build regexp expression: 1, 'ABC|ACD|XXA'
CDIVN_PATTERN as
(select dial_id,
listagg(val, '|') within group(order by dial_id) as val_pattern
from CDIVN
group by dial_id)
-- Use this expression by regexp_like
select *
from CRES c
where regexp_like(c.val,
(select '^' || p.val_pattern
from cdivn_pattern p
where p.dial_id = 1));
答案 2 :(得分:0)
你正朝着正确的方向前进,只需要加入两个表,而不需要子查询,你可以直接使用LIKE dial_pattern||'%'
。
SQL> SELECT a.*, b.*
2 FROM CRES A
3 JOIN CDIVN b
4 ON (b.DIAL_PATTERN_ID = A.profile_id)
5 WHERE TO_CHAR(8103434563) LIKE dial_pattern||'%';
PROFILE_ID PROFILE_NAME DIALLED_PATTERN DIAL_PATTERN_ID DIAL_PATTER OTHERS
---------- ------------ --------------- --------------- ----------- ------
1 A 1 1 810 abc
答案 3 :(得分:0)
如果我理解正确,我会选择:
SELECT dia
FROM CRES
WHERE pro_id = 2
AND exists (
SELECT null
FROM CDIVN
WHERE dial_id = 1 and
TO_CHAR(8103434563) like dial_pattern||'%');