![]() |
Help With Join - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Help With Join (/showthread.php?tid=48708) |
Help With Join - El Forum - 01-25-2012 [eluser]ibnclaudius[/eluser] I have two tables: Code: CREATE TABLE IF NOT EXISTS `schools` ( Code: CREATE TABLE IF NOT EXISTS `users_schools` ( I want to select every school from 'schools' where 'schools'.'id' = 'users_schools'.'schools_id' and 'users_schools'.'users_id' = $this->session->userdata('id') I'm trying to do like this: Code: $query = $this->db->select('*')->from($this->schools_table) What am I doing wrong? Help With Join - El Forum - 01-25-2012 [eluser]Aken[/eluser] You're missing the periods connecting your table names and columns. Code: $query = $this->db->select('*')->from($this->schools_table) I'm not sure if the joins are 100% correct, but I noticed that right away. Give it a shot. By the way, you can use $this->db->last_query() to see what the attempted query looks like, and what might be wrong. Help With Join - El Forum - 01-25-2012 [eluser]CroNiX[/eluser] try Code: $query = $this->db Help With Join - El Forum - 01-25-2012 [eluser]Aken[/eluser] You missed the periods, too, CroNIX ![]() EDIT: Nevermind, you noticed that. Haha. Help With Join - El Forum - 01-25-2012 [eluser]CroNiX[/eluser] Yeah, I did. And your code is overly verbose ![]() Help With Join - El Forum - 01-25-2012 [eluser]Aken[/eluser] Yeah, I honestly didn't look over the JOIN structure, just noticed the missing periods. ![]() Help With Join - El Forum - 01-26-2012 [eluser]ibnclaudius[/eluser] This code: Code: $query = $this->db->select('*')->from($this->schools_table)->join($this->users_schools_table, $this->users_schools_table . '.users_id = ' . $user_id . ' AND ' . $this->users_schools_table . '.schools_id = ' . $this->schools_table . '.id')->get(); is giving this error: Quote:SELECT * FROM (`schools`) JOIN `users_schools` ON `users_schools`.`users_id` = `4` AND users_schools.schools_id = schools.id Help With Join - El Forum - 01-26-2012 [eluser]ibnclaudius[/eluser] Fixed with Code: $query = $this->db->select('*')->from($this->schools_table)->join($this->users_schools_table, $this->users_schools_table . '.schools_id = ' . $this->schools_table . '.id')->where($this->users_schools_table . '.users_id', $user_id)->get(); Shouldnt I use DISTINCT, GROUP BY or something like that? Help With Join - El Forum - 01-26-2012 [eluser]Faisal Alghamdi[/eluser] it will be easy with active record and something like this. .. Code: $this->db->select('*'); using active records is the best way in terms of debugging the code. |