Search code examples
codeignitercodeigniter-2

Codeigniter: Join with 'where in' clause


I want to join tables to list books of two categories fiction and romance. How do I do a JOIN with a WHERE IN clause (is that even possible? sorry, noob here)

Here's where I'm stuck at:

$categories = array(10,12);
$query = select()->from('books');
$query->join('genre_map','genre_map.gid IN ('.implode(",", $categories).')');

Jn Table genre_map: bookid, genre_id

Any SQL Query/CodeIgniter help would be much appreciated.


Solution

  • never used codeigniter, but I would say

    $categories = array('10', '12');
    $this->$db->select('*');
    $this->$db->from('books');
    $this->$db->join('genre_map','genre_map.bookid = books.bookid');
    $this->$db->where_in('genre_map.gid', $categories);
    
    $query = $this->db->get();