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 8361877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:48:18+00:00 2026-06-09T11:48:18+00:00

I have a search query that is able to sort results by relevance according

  • 0

I have a search query that is able to sort results by relevance according to how many of the words from the query actually show up.

SELECT id, 
       thesis 
FROM   activity p 
WHERE  p.discriminator = 'opinion' 
       AND ( thesis LIKE '%gun%' 
              OR thesis LIKE '%crucial%' ) 
ORDER  BY ( ( CASE 
                WHEN thesis LIKE '%gun%' THEN 1 
                ELSE 0 
              end ) 
          + ( CASE 
                WHEN thesis LIKE '%crucial%' THEN 1 
                ELSE 0 
              end ) ) 
           DESC 

This query however, does not sort according to how many times ‘gun’ or ‘crucial’ show up. I want to make it so records with more occurrences of ‘gun’ show up above records with less occurrences. (I.E, add a point for every time gun shows up rather than adding a point because gun shows up at least once)

  • 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-09T11:48:19+00:00Added an answer on June 9, 2026 at 11:48 am

    Depending on how your database is set up, you may find MySQL’s full text indexing to be a better fit for your use case. It allows you to index fields and search for words in them, ordering the results by relevance related to the number of occurrences.

    See the documentation here: http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

    This is a useful question that gives some examples, and may help: How can I manipulate MySQL fulltext search relevance to make one field more 'valuable' than another?

    Finally, if full text searches aren’t an option for you, the comment posted by Andrew Hanna on the string functions reference may do the trick: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html (search the page for “Andrew Hanna”). They create a function on the server which can count the number of times a string occurs.

    Hope this helps.

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

Sidebar

Related Questions

I have this PHP code to highlight the Query on search results. if (isset($_REQUEST['k'])){
I have this query where I can search the TABLE_GLOBAL_PRODUCTS $catglobal_sql = select p.*,
I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
I noticed that I seemingly wasn't able to sort on relevance in a default
I have a search function that executes a stored procedure and returns results. If
I have a simple MySQL query that selects all columns from the table based
I current have an expandable list that I'd like to be able to search
I'm trying to create a query that can fetch results in a many to
I have a search page like <div class=> <input id=search-input type=text class=input-medium search-query span4>
I have a dynamic search query with join multiple tables, for showing sum and

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.