SQL Using Like to Search for

SQL Using Like to Search for

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?

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes