# SQL For 'LIKE' based search relevance

**URL:** https://forum.kirupa.com/t/sql-for-like-based-search-relevance/260803
**Category:** Uncategorized
**Created:** [May 20, 2008, 2:40am UTC](https://forum.kirupa.com/t/sql-for-like-based-search-relevance/260803 "2008-05-20T02:40:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lorren\_biffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/lorren_biffin/32/2726_2.png) [@lorren\_biffin](https://forum.kirupa.com/u/lorren_biffin)
#### Post date: [May 20, 2008, 2:40am UTC](https://forum.kirupa.com/t/sql-for-like-based-search-relevance/260803/1 "2008-05-20T02:40:34Z")

</div>

I’m writing a search script into a product inventory list I’ve got. I’ve got the search working great, but I want order the results by search relevance, as opposed to by a given column.

Anybody got a technique/algorithm to use here? (I’m not interested in a full search engine solution, just a method/class/advice/etc.)

Here’s my code (albeit it’s very messy/sloppy…it’s a few minutes worth of coding on a bad day and a tight deadline.

```php

    function search_products($string = NULL, $column = 'Description', $rows = '20') {
        if(isset($string) && is_string($string)) {
            $args = explode(' ', $string);
            $arg = " WHERE";
            $i = 1;
            foreach($args as $item) {
                $arg .= ($i == 1) ? "" : " OR";
                $arg .= " `$column` LIKE '%$item%'";
                $i++;
            }
            $query = "SELECT * FROM `$this-&gt;prod_table`";
            $query .= ($arg == NULL) ? '' : $arg;
            $query .= ($rows == FALSE) ? '' : ' LIMIT '.$rows;
            $query .= ";";
            return $this->wpdb->get_results($query, OBJECT);
        } else {
            return false;
        }
    }

```

---

<div class="post-metadata">

### Author: ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)
#### Post date: [May 20, 2008, 2:44am UTC](https://forum.kirupa.com/t/sql-for-like-based-search-relevance/260803/2 "2008-05-20T02:44:21Z")

</div>

Using LIKE is slower than using MATCH(someCol) AGAINST (‘str\*’ IN BOOLEAN MODE) and then you can order them by search relevance. I’m sure there’s some way to do similar thing for LIKE, but if I were you, I’d use MATCH … AGAINST …

Please see MySQL manual for MATCH … AGAINST …

Hope this helps. 🙂

---

<div class="post-metadata">

### Author: ![lorren\_biffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/lorren_biffin/32/2726_2.png) [@lorren\_biffin](https://forum.kirupa.com/u/lorren_biffin)
#### Post date: [May 20, 2008, 3:00am UTC](https://forum.kirupa.com/t/sql-for-like-based-search-relevance/260803/3 "2008-05-20T03:00:39Z")

</div>

Good deal, thanks 🙂 I’d considered MATCH, but in my haste chose LIKE instead.

I’ll update the code in a few moments.
