1
SELECT *, SUM(tbl.relevance) AS relevance FROM
(
    (
        SELECT q_id,
        MATCH(a_content) AGAINST ('бутон') AS relevance
        FROM answers

        WHERE
        MATCH(a_content) AGAINST ('бутон')
    )
    UNION
    (
        SELECT q_id,
        (MATCH(q_content) AGAINST ('бутон')) * 1.5 AS relevance
        FROM questions

        WHERE
        MATCH(q_content) AGAINST ('бутон')
    )
) AS tbl

JOIN questions ON questions.q_id = tbl.q_id

GROUP BY tbl.q_id
ORDER BY relevance DESC
4

2 回答 2

6

Codeigniter目前不支持其Active Records类中的子查询。

你只需要使用这个:

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

请记住传递第二个参数,这样Codeigniter就不会试图逃避您的查询。

于 2012-05-08T16:57:16.527 回答
4

那么对于codeigniter中的子查询确实有一个简单的hack。您必须执行以下操作 转到 system/database/DB_active_rec.php

如果您使用的是 2.0 或更高版本,请在此处更改

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

删除公共关键字,您将能够使用子查询现在

$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();
于 2012-05-11T06:22:16.303 回答