• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Table Select / Join?

#1
[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 ... ?

#2
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.