为什么这个oracle查询返回空结果集?

时间:2012-11-12 17:26:28

标签: sql oracle oracle11g

我有两个数据库表,其中包含一些演示数据,如下所示

Create table demo(uuid int, addressname varchar(50));

insert into demo values(1, 'intersportprofi');
insert into demo values(2, 'intersportprofi');
insert into demo values(3, 'intersportprofi');
insert into demo values(4, 'intersportmarket');
insert into demo values(5, 'intersportmarket');
insert into demo values(6, 'intersportmarket');

create table demo_av(uuid int, testid int, name varchar(50), value varchar(50));

insert into demo_av values(1, 1, 'sport','football basketball cricket');
insert into demo_av values(2, 1, 'brand','reebok  addidas nike');
insert into demo_av values(3, 2, 'sport','football basketball ');
insert into demo_av values(4, 2, 'brand','reebok  addidas ');

我编写了以下查询以从这些表中获取结果,但是oracle返回空结果集。

SELECT  d.addressname  FROM demo d, demo_av dv
WHERE  d.uuid = dv.testid  AND d.addressname='intersportprofi' 
AND REGEXP_LIKE( dv.value, 'reebok') AND REGEXP_LIKE( dv.value, 'cricket') 

为什么呢?我在哪里做错了?任何帮助都将受到极大关注

2 个答案:

答案 0 :(得分:5)

改变这个:

AND REGEXP_LIKE( dv.value, 'reebok') AND REGEXP_LIKE( dv.value, 'cricket') 

对此:

AND (REGEXP_LIKE( dv.value, 'reebok') OR REGEXP_LIKE( dv.value, 'cricket'))

由于:

"demo_av"表格中没有与"reebok" AND "cricket"匹配的记录。您需要的运算符为"OR",并且由于存在第一个条件,所以 必要 的parantheses。

<强>更新

以下是结果的捕获屏幕:

enter image description here

干杯

答案 1 :(得分:3)

根据您的评论,我认为您需要一个搜索多个行且具有相同testid的查询。这可以通过连接完成,或者像这样:

SELECT DISTINCT d.addressname  
FROM demo AS d
WHERE d.addressname = 'intersportprofi' 
  AND EXISTS
      ( SELECT *
        FROM demo_av AS dv
        WHERE d.uuid = dv.testid 
          AND dv.value LIKE '%reebok%'
      )
  AND EXISTS
      ( SELECT *
        FROM demo_av AS dv
        WHERE d.uuid = dv.testid 
          AND dv.value LIKE '%cricket%'
      ) ;