0
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

I am able to write the query for pretty much ordey by asd desc but how to write this complex query in codeigniter?

Shivang
  • 186
  • 2
  • 11
  • see my answer this will help http://stackoverflow.com/questions/10502778/how-can-i-rewrite-this-sql-into-codeigniters-active-records/10546395#10546395 – Muhammad Raheel Aug 29 '12 at 04:26

2 Answers2

0

You can write it in plain sql like you have it and execute it with the db->query method.

$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 query bindings explains the ?

dm03514
  • 54,664
  • 18
  • 108
  • 145
0

Maybe you should check ActiveRecord:

$this->db->
   from("abc")->
   or_like("xyz",$x)->
   or_like(...)->
   order_by(...);
$result = $this->db->result_array();
uzsolt
  • 5,832
  • 2
  • 20
  • 32