简体   繁体   中英

Why this oracle query returning empty result set?

I have two database tables with some demo data like shown below

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

I wrote the following query to get the results from those tables, but oracle returning empty result set.

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

Why? where i am doing wrong ? Any help will be greatly appriciated

Change this:

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

To this:

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

Because:

You have no record in the "demo_av" table that matches with "reebok" AND "cricket" . The operator you need is "OR" and the parantheses are necessary because of existing of the first condition.

UPDATE

Here is the capture screen of the results:

在此处输入图片说明

Cheers

Based on your comments, I think you want a query that will search over multiple rows with same testid . This can be done with joins or like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM