Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 301
Deepak Thomas Avatar asked Jun 03 '13 10:06

Deepak Thomas


1 Answers

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();
like image 147
Raphaël Althaus Avatar answered Sep 30 '22 09:09

Raphaël Althaus