How can I rewrite this SQL into CodeIgniter's Active Records?

前端 未结 2 1754
眼角桃花
眼角桃花 2020-12-04 01:11
SELECT *, SUM(tbl.relevance) AS relevance FROM
(
    (
        SELECT q_id,
        MATCH(a_content) AGAINST (\'бутон\') AS relevance
        FROM answers

        W         


        
相关标签:
2条回答
  • 2020-12-04 01:35

    Well there is indeed a simple hack for subqueries in codeigniter. You have to do the following Go to system/database/DB_active_rec.php

    There if you are using version 2.0 or greater change this

    public function _compile_select($select_override = FALSE)
    public function _reset_select()
    

    Remove public keyword and you will be able to use subqueries And now

    $data   =   array(
                 "q_id",
                 "MATCH(a_content) AGAINST ('?????') AS relevance"  
             );
    
    $this->db->select($data);
    $this->db->from("answers");
    $this->db->where("MATCH(a_content) AGAINST ('?????')");
    $subQuery1 = $this->db->_compile_select();
    // Reset active record
    $this->db->_reset_select();   
    
    unset($data);
    $data   =   array(
                "q_id",
                "(MATCH(q_content) AGAINST ('?????')) * 1.5 AS relevance"   
            );
    
    $this->db->select($data);
    $this->db->from("questions");
    $this->db->where("MATCH(q_content) AGAINST ('?????')");
    $subQuery2 = $this->db->_compile_select();
    // Reset active record
    $this->db->_reset_select(); 
    
    unset($data);
    $data   =   array(
              "q_id",
              "SUM(tbl.relevance) AS relevance" 
             );
    
    $this->db->select($data);
    $this->db->from("$subQuery1 UNION $subQuery2");
    $this->db->join("questions","questions.q_id = tbl.q_id");
    $this->db->group_by("tbl.q_id");
    $this->db->order_by("relevance","desc");
    $query = $this->db->get();
    
    0 讨论(0)
  • 2020-12-04 01:39

    Codeigniter currently does not have support for subqueries in its Active Records class.

    You'll simply have to use this:

    $this->db->query($your_query, FALSE);
    

    Remember to pass that second argument, so that Codeigniter doesn't try to escape your query.

    0 讨论(0)
提交回复
热议问题