let's say I have this table:
id | title | description | state_name
-------------------------------------------------------
01 | dairy products | has milk in it | east java
02 | school supplies | for teachers | west java
03 | school books | books for school | central java
I want to build a query that sort the result by number of matches from a string in multiple fields.
the rules are:
- order by most number of matching words
- order by the position of matched words (found on nth character in field)
for example, the keyword java school book
should result in the order id 03, 02 because id 03 has 3 matching words and id 02 only has 2 matching words.
this answer suggested using LOCATE function
from mysql but I don't know how to use that function in codeigniter 3.
I tried this query:
$search = $this->input->get('q', true);
$search = $this->db->escape($search);
$this->db->select('id, title, description, state_name');
$this->db->from('mytable');
$this->db->like('title', $search);
$this->db->like('description', $search);
$this->db->like('state_name', $search);
$this->db->order_by("LOCATE({$search}), title");
$this->db->order_by("LOCATE({$search}), description");
$this->db->order_by("LOCATE({$search}), state_name");
when I tried to search for the keyword 'test', it gives the error:
Incorrect parameter count in the call to native function 'LOCATE'
...ORDER BY LOCATE('test'), `mytable`.`title`, LOCATE('test'), `mytable`.`description`, LOCATE('test'), `mytable`.`tipsntrick`
can anyone help me?