Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.04 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Codeigntier Active Record Join
  2. Things                                        Likes
  3. ----------------------------------------     ---------------------------
  4. |  thing_id  |   thing_name |  user_id |     | thing_id   |   user_id  |
  5. ----------------------------------------     ---------------------------
  6. |     1      |    Thing 1   |   5555   |     |    2       |    6666    |
  7. |     2      |    Thing 2   |   5555   |     |    3       |    7777    |
  8. |     3      |    Thing 3   |   5555   |     ---------------------------
  9. |     4      |    Thing 4   |   5555   |
  10. ----------------------------------------
  11.        
  12. $sql = $this->db->where('things.user_id', $user_id)->get('things')->result();
  13.        
  14. $sql = $this->db->select('things.thing_id as thing_id, COUNT(likes.thing_id) as likes')
  15.                     ->where("things.user_id", $user_id)
  16.                     ->from('things')
  17.                     ->join('likes', 'things.thing_id = likes.thing_id', 'LEFT')
  18.                     ->group_by('likes.thing_id')
  19.                     ->get()
  20.                     ->result();