CodeIgniter Forums
Multiple Table Select / Join? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Multiple Table Select / Join? (/showthread.php?tid=37147)



Multiple Table Select / Join? - El Forum - 12-29-2010

[eluser]shenanigans01[/eluser]
I have two tables.

Users - email
Meta - company

Meta has a "user_id" field that matches the "id" field in the users table.
basically i'd like to select "emails" from "users" where "company" = 1; ... but I'm not sure how to span across both tables like that, or if its even possible ... ?


Multiple Table Select / Join? - El Forum - 12-29-2010

[eluser]Madmartigan1[/eluser]
Try this:

Code:
$emails = $this->db
      ->select('users.email')
      ->join('meta', 'meta.user_id = users.id', 'left')
      ->where('meta.company', $company)
      ->get('users');

JOIN is a tricky thing to get the hang of, but well worth the effort to learn.
I would suggest reading on nettuts or something, the sql manual is not a place for beginners.

Taks a look at some JOIN examples on the CI User Guide Active Record section as well.