让我说我有这张桌子:
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
我想构建一个查询,根据多个字段中字符串的匹配数对结果进行排序。
规则是:
例如,关键字java school book
应该产生订单ID 03,02因为id 03有3个匹配的单词而id 02只有2个匹配的单词。
this answer建议使用mysql中的LOCATE function
,但我不知道如何在codeigniter 3中使用该功能。
我尝试了这个查询:
$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");
当我尝试搜索关键字' test'时,它会出错:
Incorrect parameter count in the call to native function 'LOCATE'
...ORDER BY LOCATE('test'), `mytable`.`title`, LOCATE('test'), `mytable`.`description`, LOCATE('test'), `mytable`.`tipsntrick`
任何人都可以帮助我吗?