devxlogo

SQL Using Like to Search for

SQL Using Like to Search for

Question:
I have created a database of journal articles and want to offer users the ability to search the Author field for the surnames of authors. Most of the articles have more than one author so the Author field usually contains more than one surname.

I set it up so that users could choose to search the Author field for “any of the words” or “all the words.” The SQL query for a search on the surnames “Eden” and “Ackermann” looks like this:

 SELECT *FROM Research WHERE ( Title like '%%' ) AND   ( Author like '%Eden%' AND   Author like '%Ackermann%') ORDER BY Number DESC

This works fine for “any of the words” and “all the words” for these authors but there is an author with the surname “Li” in the database and using the wildcard “%” before the name in the SQL query brings back a lot of names which contain the string “Li.”

If I change things (see below) I hit problems when the user searches for “all the words”:

  • If I drop the wildcard from in front of the surnames so that the SQL query looks like this:

    ( Author like 'Eden%' AND Author like 'Ackermann%')

    then I get no results back!

  • If I leave the wildcard in front of one of the author’s surnames, such as

    ( Author like 'Eden%' AND Author like '%Ackermann%')

    then different results are returned depending on whether they entered Eden or Ackermann first (some of the articles in the database list Eden as first author and some list Ackermann as first author).

Any ideas on how to get around this problem?

Answer:
Yes, but I don’t think you are going to like it.

I think you need to redesign your tables so that you achieve the following:

  • Store only one author’s name and ID per row in a table called Authors.
  • Store only one book title and ID per row in a table named Titles.
  • Relate authors to the titles in a new table called AuthorTitle. This table takes one title and matches it against at least one author name, more if necessary.
  • Designate one of the rows for each title in AuthorTitle as “Lead Author” with a bit and mark it True for the lead author.

Now your searches will be a lot simpler because you won’t have the embedded match problem, which arises because “%” means “Match any.”

devx-admin

Share the Post: