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

  • Home
  • SEARCH
  • 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 5841831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:56:24+00:00 2026-05-22T11:56:24+00:00

I am trying to write a SQL query to fetch rows whose fields contain

  • 0

I am trying to write a SQL query to fetch rows whose fields contain multiple key words. The use for the query is to fetch results for an autocompleter function implemented with jQuery.

Right now, the autocompletion works, but my results will include every row whose content in a specific field contains the query. For example, if I type in the phrase ‘ar’, my results will include car, guitar , bark, etc.

I would like to return only results that begin with the phrase, as users will most likely not be typing in random fragments of the middle of words. I can fetch all rows where the first word begins with the query, by using Model.where((:name =~ "#{params[:q]}%") [I am using the meta_where gem].

However, I’d like to also return rows where the second word in the field begins with the query string, even though the first word does not.

For example, for the rows “black dog” and “dog food”, the query string ‘do’ should return both of the rows, while the query string ‘do’ should not return ‘undone’.

How should I write the SQL query for a search like this?

  • 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-05-22T11:56:25+00:00Added an answer on May 22, 2026 at 11:56 am
    Model.where( (:name =~ "#{params[:q]}%") | (:name =~ "% #{params[:q]}%") )
    

    This construction works with meta_where plugin only

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

Sidebar

Related Questions

I am trying to write a query for SQL Server 2005 but I can't
I am trying to write a sql query which fetches all the tags related
I am trying to write a SQL query where I am joining two tables
I'm trying to write a query in Oracle SQL to access a particular substring
I'm trying to write a SQL query to generate a summary row for the
I am trying to write a SQL query that does some complex sorting. I
Can anyone help me with the trying to write SQL (MS SqlServer) - I
I'm trying to write some SQL that will delete files of type '.7z' that
I'm trying to write some LINQ To SQL code that would generate SQL like
I'm trying write a query to find records which don't have a matching record

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.