how to use join query in codeigniter

Sure! Here is an example of how to use a join query in CodeIgniter:

$this->db->select('users.name, orders.order_number');
$this->db->from('users');
$this->db->join('orders', 'users.id = orders.user_id', 'inner');
$query = $this->db->get();

Let's go through each step:

  1. Selecting columns: First, you need to specify the columns you want to select from the tables involved in the join query. In this example, we are selecting the name column from the users table and the order_number column from the orders table. You can add more columns by separating them with commas.

  2. Specifying the main table: Next, you need to specify the main table from which you want to retrieve data. In this case, the main table is users. You can use the from() method to specify the table.

  3. Joining tables: To join tables, you can use the join() method. The first parameter is the name of the table you want to join, which is orders in this example. The second parameter is the join condition, which specifies how the tables are related. In this case, we are joining the users table with the orders table using the condition users.id = orders.user_id. This means that we are joining the tables where the id column in the users table matches the user_id column in the orders table.

  4. Specifying the join type: The third parameter of the join() method is the join type. In this example, we are using the inner join type, which returns only the rows where there is a match in both tables. Other join types you can use are left, right, and outer.

  5. Executing the query: Finally, you can execute the query using the get() method. This will return the result set that matches the join condition specified.

I hope this helps! Let me know if you have any further questions.