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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:11:36+00:00 2026-06-14T05:11:36+00:00

I have a table, InnoDB, with columns id(int), name(varchar) and info(text). I need to

  • 0

I have a table, InnoDB, with columns id(int), name(varchar) and info(text). I need to search the name and info columns of the table using a given term and rank the matching records from high to low, according to the number of ‘hits’ in the two columns combined for each record.

I’ve trawled about but I’ve never ever done anything like this with MySQL before and find it all a little confusing… case and match etc. etc.

Can anyone offer some quick help?

This is as far as I’ve got:

SELECT count(*) as count
FROM artistmanager_artists WHERE
case when 'name' LIKE '%a%' then 1 else 0 end
+ case when 'info' LIKE '%a%' then 1 else 0 end
ORDER BY count ASC

(‘a’ being the search term)

Which returns one row and one column, ‘count’, with a value of three… which I’m guessing is because I’ve got three records in the table at the mo?

I’ve also found and tried this:

SELECT *
FROM `artistmanager_artists`
WHERE `name` LIKE '%a%'
AND (
   `info` LIKE '%a%'
)
ORDER BY (
  (
      CASE WHEN `name` LIKE '%a%'
      THEN 1
      ELSE 0
      END
  ) + (
     CASE WHEN `info` LIKE '%a%'
     THEN 1
     ELSE 0
     END
  )
) DESC

This returned all but one record, in no obvious order, when I KNOW the record it didn’t return contains a lot of ‘a’!

  • 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-14T05:11:38+00:00Added an answer on June 14, 2026 at 5:11 am

    I have done it! Thanks to @MvG for the inspiration. Since I’m much better with my PHP than I am my MySQL I solved the problem by doing the following: (I’m using Codeignite btw)

    public function get_artists_search($search_term)
    {
        $artists = array();
        $query = $this->db->query('SELECT id, name, info, (name LIKE "%' . $search_term . '%") + (info LIKE "%' . $search_term . '%") AS rnk FROM artistmanager_artists ORDER BY rnk DESC');
        foreach ($query->result() as $row)
        {
            // returned rows have at least one occurence in at least one of the two columns, so values are 1 or 2
            $artists[$row->id]['name'] = $row->name;
            $artists[$row->id]['info'] = $row->info;
            $artists[$row->id]['score'] = 0;
        }
        // now foreach returned row find occurence values.
        foreach($artists AS $key=>$artist)
        {
            $score_name = substr_count(strtoupper($artist['name']), strtoupper($search_term));
            $score_info = substr_count(strtoupper($artist['info']), strtoupper($search_term));
            $artists[$key]['score'] = $score_name+$score_info;
            unset($artists[$key]['name']);
            unset($artists[$key]['info']);
        }
        arsort($artists);
    }
    

    Which returns an array with the ids of the records as key and a rank value, sorted high to low. I tested it and it works so I’m chuffed.

    Thanks to all for help with the lateral thinking!

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

Sidebar

Related Questions

I have a MySQL InnoDB table with 238 columns. 56 of them are TEXT
I have the following MySQL (version 5.1) table (InnoDB): Username varchar(50) Sequence int(11) FileType
I have a fairly static (InnoDB) table T with four columns: A , B
I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have a search functionality that obtains data from an InnoDB table ( utf8_spanish_ci
I have a MySQL InnoDB table laid out like so: id (int), run_id (int),
We have a very large (10million+) row table stored in MySql using the InnoDB
I have a MySQL InnoDB table, events , with 3 rows: event_id ---> int
I have a InnoDB table that has about 17 normalized columns with ~6 million
I have a relatively big MySQL InnoDB table (compressed), and I sometimes need to

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.