You are on page 1of 3

i need to select name from office_tb and name from office_branch_tb but the name appear to be

the same how can i modify my code

$sql = "SELECT *

FROM staff_tb

JOIN designation_tb ON staff_tb.designation_id = designation_tb.id

JOIN office_branch_tb ON staff_tb.office_branch_id = office_branch_tb.id";

If you want to display the name of the office and the name of the branch separately, you can modify
your query to select the name field from both tables using aliases. Here's an example:

bash

Copy code

$sql = "SELECT

staff_tb.*,

designation_tb.name AS designation_name,

office_tb.name AS office_name,

office_branch_tb.name AS branch_name

FROM staff_tb

JOIN designation_tb ON staff_tb.designation_id = designation_tb.id

JOIN office_branch_tb ON staff_tb.office_branch_id = office_branch_tb.id

JOIN office_tb ON office_branch_tb.office_id = office_tb.id";

In this query, we are selecting all fields from the staff_tb table and also selecting the name fields
from the designation_tb, office_tb, and office_branch_tb tables using aliases (designation_name,
office_name, and branch_name). We are also joining the office_tb table to get the name of the
office.

You can then use these aliases in your PHP code to display the names separately. For example:

php

Copy code

<td><?php echo htmlentities($row->office_name);?> - <?php echo htmlentities($row-


>branch_name);?></td>

This will display the name of the office and the name of the branch separated by a hyphen.
Dogan Robert

what if i want to consider by selection other column on three tables?

If you want to select data based on a specific column from multiple tables, you can add a WHERE
clause to your SQL query. Here's an example:

bash

Copy code

$sql = "SELECT

staff_tb.*,

designation_tb.name AS designation_name,

office_tb.name AS office_name,

office_branch_tb.name AS branch_name

FROM staff_tb

JOIN designation_tb ON staff_tb.designation_id = designation_tb.id

JOIN office_branch_tb ON staff_tb.office_branch_id = office_branch_tb.id

JOIN office_tb ON office_branch_tb.office_id = office_tb.id

WHERE staff_tb.active = 1";

In this example, we are selecting data from the staff_tb, designation_tb, office_tb, and
office_branch_tb tables based on the condition that the active column in the staff_tb table is equal
to 1. You can replace staff_tb.active = 1 with any condition that you want to use to filter the data.

Note that in this query, we are still selecting all fields from the staff_tb table and the name fields
from the other tables using aliases. You can modify the query and the aliases based on your specific
needs.

$sql = "SELECT
staff_tb.id,staff_tb.first_name,staff_tb.middle_name,staff_tb.last_name,staff_tb.mobile,staff_tb.pf_
no,staff_tb.email,staff_tb.photo,staff_tb.Regdate,staff_tb.status,staff_tb.password,staff_tb.designati
on_id,staff_tb.office_branch_id,designation_tb.name as designation_name,office_branch_tb.name
as branch_name,office_tb.name AS office_name

FROM staff_tb
INNER JOIN office_branch_tb ON staff_tb.office_branch_tb_id =office_branch_tb.id

INNER JOIN office_tb ON office_branch_tb.office_tb_id = office_tb.id

INNER JOIN designation_tb ON staff_tb.designation_tb_id = designation_tb.id

WHERE staff_tb.id = '$id'";

SELECT staff_tb.id, staff_tb.name, office_branch_tb.name AS branch_name, designation_tb.name AS


designation_name, office_tb.name AS office_name

FROM staff_tb

LEFT JOIN office_branch_tb ON staff_tb.office_branch_tb_id = office_branch_tb.id

LEFT JOIN office_tb ON office_branch_tb.office_tb_id = office_tb.id

INNER JOIN designation_tb ON staff_tb.designation_tb_id = designation_tb.id;

You might also like