SELECT * FROM abc
WHERE xyz LIKE $x OR xyz LIKE $y OR xyz LIKE $z
ORDER BY ((xyz LIKE $x) + (xyz LIKE $y) + (xyz LIKE $z)) DESC
我几乎可以编写查询,ordey by asd desc
但是如何在 codeigniter 中编写这个复杂的查询?
SELECT * FROM abc
WHERE xyz LIKE $x OR xyz LIKE $y OR xyz LIKE $z
ORDER BY ((xyz LIKE $x) + (xyz LIKE $y) + (xyz LIKE $z)) DESC
我几乎可以编写查询,ordey by asd desc
但是如何在 codeigniter 中编写这个复杂的查询?
您可以像拥有它一样用普通的 sql 编写它并使用该db->query
方法执行它。
$sql = 'SELECT * FROM abc
WHERE xyz LIKE ? OR xyz LIKE ? OR xyz LIKE ?
ORDER BY ((xyz LIKE ?) + (xyz LIKE ?) + (xyz LIKE ?)) DESC'
$this->db->query($sql, array($x, $y, $z, $x, $y, $z));
http://codeigniter.com/user_guide/database/queries.html查询绑定解释了?
也许你应该检查ActiveRecord:
$this->db->
from("abc")->
or_like("xyz",$x)->
or_like(...)->
order_by(...);
$result = $this->db->result_array();