0

I am trying to get a grasp on the CI database helper/class/lib. And the core basics I can do no problem. But I am so used to straight writing of queries rather than using ORM's that its a little confusing. Anyway..

I Have this query

SELECT * FROM my_table 
  WHERE ((a_id = xx OR b_id = xx) AND (a_id = zz OR b_id = zz)) 
  AND active = 1

Basically I have a table where I have 2 users associated with one another, but multiple users so I need to make sure the connection is there and specifically between the 2 where the active flag is what it is.

However Not sure how to handle that query in CI's ORM so I am seeking some guidance on the issue.

chris
  • 36,115
  • 52
  • 143
  • 252

3 Answers3

1

Try the following code

$this->db->select('*');
$this->db->where('(a_id = "xx" or b_id = "xx")';
$this->db->where('(a_id = "zz" or b_id = "zz")';
$this->db->where('active', '1');
iLaYa ツ
  • 3,941
  • 3
  • 32
  • 48
1

Look at this answer of mine. This will give you a fairly good idea how to use subqueries or this kind of situations in active record

How can I rewrite this SQL into CodeIgniter's Active Records?

Community
  • 1
  • 1
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
1

If you don't want to use active records you can do it this way

$this->load->database();
$sql = "SELECT * FROM mytable WHERE visible = 't' ORDER BY symbol";
$query = $this->db->query($sql);
return $query->result();

See http://codeigniter.com/user_guide/database/queries.html

Matt Urtnowski
  • 2,556
  • 1
  • 18
  • 36