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
Answer 1:
笨目前没有在它的活动记录类的子查询的支持。
你将只需要使用:
$this->db->query($your_query, FALSE);
记住要传递第二个参数,这样笨不会去逃避查询。
Answer 2:
那么有的确是在笨子查询一个简单的黑客。 你必须做到以下几点进入系统/数据库/ 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();
Answer 3:
你有没有尝试这个查询而不子查询? 我认为你能做到不与子查询的使用left join
S和使用IS NOT NULL
。
文章来源: How can I rewrite this SQL into CodeIgniter's Active Records?