1

我有两个带有一些演示数据的数据库表,如下所示

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') 

为什么?我在哪里做错了?任何帮助将不胜感激

4

2 回答 2

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"并且括号是必要的。

更新

这是结果的捕获屏幕:

在此处输入图像描述

干杯

于 2012-11-12T17:28:39.567 回答
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%'
      ) ;
于 2012-11-12T19:10:51.843 回答