Using 'Like' to query related fields between tables

G

Guest

'Like' to query related field; ie [db1.serial] like [db2.serial]


I am trying to query on related fields between two tables, one [db1.serial]
which has multiple data elements separated by commas within the field, and
the other [db2.serial] having a single data element. I want to find the
single data element within the field with multiple data elements.

Any suggestions?

Kevin
 
M

Marshall Barton

Kevin C. said:
'Like' to query related field; ie [db1.serial] like [db2.serial]


I am trying to query on related fields between two tables, one [db1.serial]
which has multiple data elements separated by commas within the field, and
the other [db2.serial] having a single data element. I want to find the
single data element within the field with multiple data elements.


SELECT . . .
FROM db1 INNER JOIN db2
ON "," & db1.Serial & "," Like "*," & db2.Serial & ",*"
 
T

Tom Ellison

Dear Kevin:

The answer may be partially to do this:

WHERE "," & db1.serial & "," LIKE "*," & db2.serial & ",*"

If the list is:

A,B,C,something

and you are searching for "something" then it searches for:

,something,

in the list

,A,B,C,something,

That is, it adds the commas to make the search consistent.

However, if db1.serial is entered by a user typing this list, there are
multiple problems:

- users will commonly, but not necessarily consistently, put a space after
the comma. That blows the above method away completely, unless you want to
search for all possible combinations of the space missing or not. That's 4
combinations, and therefore 4 searches.

- this search cannot use indexing, and forces a table scan. With the above,
it may well be 4 table scans. That makes for terrible performance.

The solution is to structure your database with a separate, dependent
(related one-to-many) table containing one row for each of the items listed
in db1.serial. This can then be indexed, and the search can be made just
once and using indexing. Solves the performance problem to the best
possible.

Finally, you should (if possible) keep a list of values that are permitted
in this set. Allow users to choose from this list, adding to it only when
necessary. If you're going to search this, it would be very helpful if you
enforce consistent spelling of the things in the list. Mis-spelled items in
the list, or mis-spelled keys for which you search, these will create
inconsistent results. The search itself should be driven by a list of all
the values used so far.

By the way, do not write [db1.serial] if you mean the column [serial] in
table [db1]. It must be written [db1].[serial] or it can be written
db1.serial. [db1.serial] would refer to a column whose name includes the
period. Not nice!

Tom Ellison
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top