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.”

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job