CodeIgniter Forums
build query for 2 different table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: build query for 2 different table (/thread-80218.html)



build query for 2 different table - zhikri - 09-30-2021

i have 2 table, profiles and details. 

trackid in details table is a foreign key that references to trackid in profiles table which is a primary key 
Code:
profiles table:
trackid| department |
---+----------+-----+
1      | IT         |
2      | Marketing  | 
3      | Sales      | 

details table:

ID | trackid | department    | employee
---+----------+---------+---------
1  | 1       | IT            | Ruben 
2  | 2       | Marketing     | Diaz
3  | 3       | Sales         | John
4  | 1       | IT            | Lucy
5  | 3       | Sales         | Anna

first i have list all of department from profiles table, then i put detail button to show detail from each department.
so when it clicked, it will redirect to the details page of clicked department and show number of employee and its name from details table.

is there any suggestion to build query in models for this case?
thank you


RE: build query for 2 different table - includebeer - 10-04-2021

Look for the join() function on this page: https://codeigniter.com/user_guide/database/query_builder.html

PHP Code:
$builder $db->table('blogs');
$builder->select('*');
$builder->join('comments''comments.id = blogs.id');
$query $builder->get();

// Produces:
// SELECT * FROM blogs JOIN comments ON comments.id = blogs.id