codeigniter中的mysql最佳匹配算法

时间:2016-12-20 10:11:37

标签: php mysql codeigniter

让我说我有这张桌子:

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

我想构建一个查询,根据多个字段中字符串的匹配数对结果进行排序。

规则是:

  1. 按大多数匹配词排序
  2. 按匹配单词的位置排序(在字段中的第n个字符上找到)
  3. 例如,关键字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`
    

    任何人都可以帮助我吗?

0 个答案:

没有答案