I have a employee table,city country and state table. My employee table stores id of country state and city. I want country_name, state_name, city_name using mysqli query in optimized form.
employee table
emp_id name email country state city
------- ---- ----- ------- ------ -----
1 abc a@gmail.com 1 1 1
country table
country_id country_name
----------- -------------
1 India
state table
state_id country_id state_name
-------- ---------- -----------
1 1 Gujarat
city table
city_id state_id city_name
------- ------- --------
1 1 Ahmedabad
Function used to fetch the data
function select_employee(){
$sth = $this->con->prepare("SELECT * from employees");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
Function to display state,city,country
function select_places(){
$sth = $this->con->prepare("SELECT country.country_name,state.state_name,city.city_name
FROM employees
LEFT JOIN country ON employees.country = country.country_id
LEFT JOIN state ON employees.state = state.state_id
LEFT JOIN city ON employees.city = city.city_id");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
printing data
<?php
foreach ($result as $values){
?>
</tr>
<td><?php echo $values['country']; ?></td>
<td><?php echo $values['state']; ?></td>
<td><?php echo $values['city']; ?></td>
}
In the print data section i need data India,Gujarat,Ahmedabad. Not their ids.
output
country_name state_name city_name
------------ -------- ----------
1 1 1
what i need
emp_id name email country_name state_name city_name
----- ---- ------- ------------ -------- ----------
1 abc ab@gm.com India Gujarat Ahmedabad
how can i prepare the query or join for that? that using id from employee i get name from country,state,city table