我在codeigniter中有一个匹配,但我需要通过最佳匹配来订购

时间:2017-03-28 15:18:40

标签: php mysql codeigniter match

您好我有这个代码,它可以工作,但匹配不会返回最佳匹配的数组。如何通过类似于$ search的顺序来命令该数组。

        $this->db->select("p.code, p.STOCK, t.TITLE, p.UNIFICADO, p.BRAND, p.DESCRIPTION, p.PRECIO, p.DEPENDENCIA");
        $this->db->from('part_info_titles t');
        $this->db->join('part_info p', 't.INTERNAL_CODE = p.NKR_CODE');
        $this->db->join('application a', 'p.UNIFICADO = a.UNIFICADO');
        $this->db->where("a.FROM_YEAR <=",$year);
        $this->db->where("a.THRU_YEAR >=",$year);
        $this->db->where("a.MODEL", $model);
        if(($engine != "all")and($engine != 'ALL')){
        $engines = array($engine, 'ALL', 'all','');
        $this->db->where_in('a.ENGINE', $engines);
        }
        $this->db->group_by('p.code'); 
        $this->db->distinct();
        $this->db->where('MATCH (t.TITLE) AGAINST ("'.$search.'")', NULL, false);

        $query = $this->db->get();
        return $query->result_array();

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

以下是使用您的样本数据测试的解决方案:

    $search = 'door mirror';
    $year = 2000;
    $model = 'NPR';
    $engine = 'all';

    $this->db->select('p.NKR_CODE, p.STOCK, t.TITLE, p.UNIFICADO, p.BRAND, p.DESCRIPTION, p.PRECIO, p.DEPENDENCIA, 
       MATCH (t.TITLE) AGAINST ("'.$search.'") AS relevance', FALSE);
    $this->db->from('part_info_titles t');
    $this->db->join('part_info p', 't.INTERNAL_CODE = p.NKR_CODE');
    $this->db->join('application a', 'p.UNIFICADO = a.UNIFICADO');
    $this->db->where("a.FROM_YEAR <=",$year);
    $this->db->where("a.THRU_YEAR >=",$year);
    $this->db->where("a.MODEL", $model);
    if(($engine != "all") and ($engine != 'ALL')){
       $engines = array($engine, 'ALL', 'all','');
       $this->db->where_in('a.ENGINE', $engines);
    }

    $this->db->where('MATCH (t.TITLE) AGAINST ("'.$search.'")', NULL, false);       

    $this->db->group_by('p.NKR_CODE'); 

    // ORDER BY RELEVANCE IN DESCENDING ORDER
    $this->db->order_by("relevance", "desc");

    $query = $this->db->get();

    // CHECK TO SE IF ANY DATA EXISTS BEFORE RETURNING...
    if($query !== FALSE && $query->num_rows() > 0){
        return $query->result_array();
    }

如果需要,在Pure SQL中:

SELECT p.NKR_CODE, p.STOCK, t.TITLE, p.UNIFICADO, p.BRAND, p.DESCRIPTION, p.PRECIO, p.DEPENDENCIA, 
       MATCH (t.TITLE) AGAINST ('door mirror') AS relevance
FROM part_info_titles t
INNER JOIN part_info p ON t.INTERNAL_CODE = p.NKR_CODE
INNER JOIN application a ON p.UNIFICADO = a.UNIFICADO
WHERE 
    a.FROM_YEAR <= 2000
    AND a.THRU_YEAR >= 2000
    AND a.MODEL = 'NPR'
#AND a.ENGINE IN('', 'ALL', 'all')
AND MATCH (t.TITLE) AGAINST ('door mirror')
GROUP BY p.NKR_CODE
ORDER BY relevance DESC;

注意:您提供的示例数据在t.INTERNAL_CODE = p.NKR_CODE中只有一个匹配,因此只返回了一行。