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
答案 0 :(得分:6)
Codeigniter 目前不支持 Active Records 类中的子查询。
你只需要使用它:
$this->db->query($your_query, FALSE);
请记住传递第二个参数,以便 Codeigniter 不会尝试转义您的查询。
答案 1 :(得分:4)
在codeigniter中确实存在一个简单的子查询攻击。你必须做以下事情 转到system / database / DB_active_rec.php
如果您使用的是2.0或更高版本,请更改此
public function _compile_select($select_override = FALSE)
public function _reset_select()
删除public关键字,您将能够使用子查询 现在
$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();
答案 2 :(得分:0)
您是否在没有子查询的情况下尝试此查询?我认为您可以在不使用left join
的子查询并使用IS NOT NULL
的情况下执行此操作。