Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8255789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:38:39+00:00 2026-06-08T01:38:39+00:00

I have the following query: SELECT * FROM table WHERE tags LIKE ‘%$search%’ AND

  • 0

I have the following query:

SELECT * FROM table 
    WHERE tags LIKE '%$search%' 
          AND tags NOT LIKE '% $search%' 
          AND tags NOT LIKE '%$search %'

Basically it’s searching for for all terms that match unless there’s a space before or after the search term.

So if I searched for "novel", the terms "graphic novel" or "novel romantic" will not show up.

This seems like a costly query, so I was wondering if there is a more efficient way to do this. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:38:41+00:00Added an answer on June 8, 2026 at 1:38 am

    A regular expression match will make the query shorter )prettier is a matter of the observer), but most definitely not more efficient.

    Looking at your comment to Sashi Kant, am I right do deduce the text on which you are searching is a comma delimited set of attributes? You wrote: big,novel,graphic novel. Is it always like that?

    If so, then, again, still not efficient but nevertheless easier to manage, is to write

    SELECT * FROM table 
      WHERE FIND_IN_SET('novel', tags) > 0
    

    What’s shared between your solution, a regex solution and the FIND_IN_SET solution is that neither can utilize any index on the tags column. All queries are using some sort of function over the column, and that negates the usage af an index.

    If you want performance, and data format is as I think it is, then you may want to normalize the table. Create a new table like known_tag:

    CREATE TABLE known_tag (
      known_tag_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
      name VARCHAR(127) CHARSET ascii
    );
    

    (pick your own data types)

    And then a many-to-many connecting table, like:

    CREATE TABLE original_table_to_known_tag (
      original_table_id INT UNSIGNED,
      known_tag_id INT UNSIGNED,
      PRIMARY KEY(original_table_id, known_tag_id),
      KEY(known_tag_id)
    );
    

    And finally, work your query like this:

    SELECT 
      table.*
    FROM 
      known_tag 
      JOIN original_table_to_known_tag USING (known_tag_id)
      JOIN original_table USING (original_table_id)
    WHERE
      known_tag.name = 'novel'
    ;
    

    This type of query will use the proper indexes and be more efficient on large tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: SELECT * FROM table_name WHERE (col_1 LIKE '%$keyword%' OR
Can I have a prepared statement with the following query: select * from table
I have the following query SELECT * FROM table WHERE id IN (5,4,3,1,6) and
I have the following SQL query: SELECT * FROM table WHERE field_1 <> field_2
Let's say I have the following query: SELECT anInteger FROM table; How do I
I have the following query: select column_name, count(column_name) from table group by column_name having
Lets say I have following query: SELECT * FROM table WHERE id = 1
I have the following MySQL query statement: SELECT * FROM table1 INNER JOIN table2
I have the following query: SELECT * FROM ( `teams` ) WHERE `name` =
I have the following query SELECT * FROM attend RIGHT OUTER JOIN noattend ON

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.