Hi there

I'm looking for help with an SQL problem. I have 2 tables which contain 'keywords' (Keyword1-6 in each table). I'm trying to find a way to efficiently find all records in one table that match one or more of the keywords extracted from a record in the other table, and allow me to sort results in order of highest match.

Sorry if its tough to follow. So for example, in one table i have some records like this:

Record 1:

Keyword1 "Architect"

keyword2 "Major projects"

Keyword3 "London"

Record 2:

Keyword1 "Architect"

Keyword2 "Small projects"

Keyword3 "Sydney"

etc.

and in the other table I have a record which I want to search with

Keyword1 "Architect"

Keyword2 "Hong Kong"

Keyword3 "Small"

say.

I can put those into a string, thus "Architect Sydney Small", and search through the first table to find all full/partial matches.

Since both records in the first table match the search at least partially, I want to return both records from the second table, sorted in order of the best match, eg.

Record2 - 3 keyword matches (one partial)

Record1 - 2 matches

Note the order of Keywords may be different in both tables as shown above. I'm just trying to find anything that matches and sort into the best match order. Hope that's clear.

I've been trying a join but without much success. Any ideas on the best approach?

Thanks guys.


In reply to Finding best matches by Scotsman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.