Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> A difficult SQL Query, MARY and FRED

A difficult SQL Query, MARY and FRED

From: barry <bbulsara23_at_hotmail.com>
Date: 15 Mar 2002 09:47:29 -0800
Message-ID: <747f1dec.0203150947.26bcca58@posting.google.com>


Hello, the following executes under Oracle 8i. The SQL displays the names 'MARY' and 'FRED' and this is the output I want to be produced. Essentially the query searches the column unorderedstring in table tblToBeSearched for a set of keywords. The rule is that each unorderedstring entry in the table will contain either 0 or 1 of the keywords and all keywords are located in table tblKeyWords. Of course what I've written here is a contrived example, the real thing is much more messy. Here's the SQL.

CREATE TABLE tblToBeSearched(unorderedstring VARCHAR2(40), fieldfound VARCHAR2(20) DEFAULT NULL);
INSERT INTO tblToBeSearched VALUES ('THE QUICK BROWN FOX JUMPS OVER...',NULL);
INSERT INTO tblToBeSearched VALUES ('PETER AND FRED WENT DOWN TO THE PUB',NULL);
INSERT INTO tblToBeSearched VALUES ('SADFSAF ASFA$$5 SFSAFHSFU2K\\',NULL);
INSERT INTO tblToBeSearched VALUES ('NO ***MARY*** DF\K\\',NULL);

CREATE TABLE tblKeyWords(keyword VARCHAR(20) PRIMARY KEY);

INSERT INTO tblKeyWords VALUES ('MARY');
INSERT INTO tblKeyWords VALUES ('HARRY');
INSERT INTO tblKeyWords VALUES ('MIKE');
INSERT INTO tblKeyWords VALUES ('FRED');


SELECT keyword FROM tblKeyWords
 WHERE EXISTS (SELECT * FROM tblToBeSearched WHERE unorderedstring LIKE '%' || keyword || '%');

This produces the correct output, two entries containing the names MARY and FRED.

However, what I really want is the keyword found 'copied' into the column fieldfound for the same row in the table. In otherwords, if I were to perform the following SQL query:

SELECT * FROM tblToBeSearched

the generated output would be:

UNORDEREDSTRING                          FIELDFOUND
---------------------------------------- --------------------
PETER AND FRED WENT DOWN TO THE PUB      FRED
SADFSAF ASFA$$5 SFSAFHSFU2K\\            
NO ***MARY*** DF\K\\                     MARY
THE QUICK BROWN FOX JUMPS OVER... Received on Fri Mar 15 2002 - 11:47:29 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US