0

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

Dhruv Thakkar
  • 415
  • 1
  • 6
  • 18

2 Answers2

3

Function

 function select_employee(){
      $select = "SELECT * from employees";
      $select .= " join country on employees.country_id = country.country_id";
      $select .= " join state on employees.state_id = state.state_id";
      $select .= " join city on employees.city_id = city.city_id";
      $sth =  $this->con->prepare($select);
      $sth->execute();
      $result = $sth->fetchAll(PDO::FETCH_ASSOC);

      return $result;
  }

Printing

<?php foreach ($result as $values): ?>
    <tr>
        <td><?php echo $values['country_name']; ?></td>
        <td><?php echo $values['state_name']; ?></td>
        <td><?php echo $values['city_name']; ?></td>
    </tr>
<?php endforeach;?>
Ermenegildo
  • 1,286
  • 1
  • 12
  • 19
  • question edited pls help.. i need also the datas from emp table, so do i have to use second function and make a select * query ? – Dhruv Thakkar Apr 16 '19 at 14:01
1

Change $sth = $this->con->prepare("SELECT * from employees"); to

$sth =  $this->con->prepare("      
SELECT employees.* country_table.country_name, state_table.state_name, city_table.city_name FROM 
   (((employees INNER JOIN country_table ON employees.country_id = country_table.country_id)
   INNER JOIN state_table ON employees.state_id = state_table.state_id)
   INNER JOIN city_table ON employees.city_id = city_table.city_id)");
xmaster
  • 1,042
  • 7
  • 20