
Untitled
By: a guest on
May 8th, 2012 | syntax:
None | size: 1.04 KB | hits: 9 | expires: Never
Codeigntier Active Record Join
Things Likes
---------------------------------------- ---------------------------
| thing_id | thing_name | user_id | | thing_id | user_id |
---------------------------------------- ---------------------------
| 1 | Thing 1 | 5555 | | 2 | 6666 |
| 2 | Thing 2 | 5555 | | 3 | 7777 |
| 3 | Thing 3 | 5555 | ---------------------------
| 4 | Thing 4 | 5555 |
----------------------------------------
$sql = $this->db->where('things.user_id', $user_id)->get('things')->result();
$sql = $this->db->select('things.thing_id as thing_id, COUNT(likes.thing_id) as likes')
->where("things.user_id", $user_id)
->from('things')
->join('likes', 'things.thing_id = likes.thing_id', 'LEFT')
->group_by('likes.thing_id')
->get()
->result();