Welcome Guest, Not a member yet? Register   Sign In
build query for 2 different table
#1

(This post was last modified: 10-04-2021, 04:13 PM by includebeer. Edit Reason: formatting )

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
Reply
#2

Look for the join() function on this page: https://codeigniter.com/user_guide/datab...ilder.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 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB