L
L
Lavrov952018-02-22 22:08:10
MySQL
Lavrov95, 2018-02-22 22:08:10

How to join from another table?

I must to join company_type add get All datas from companies.
index.php

include 'Company.php';
$company = new Company();
$companyData = $company->getCompanyById($companyId);

var_dump($companyData);

Company.php
class Company
{
   public function getCompanyById($companyId)
    {
$sql="SELECT companies.id, companies.company_type_id, companies.name, companies.email
            FROM companies
            INNER JOIN company_type
            ON company_type.name = companies.company_type_id WHERE companies.id = $companyId ";        
         $query = $this->db->pdo->prepare($sql);
        $query->bindValue(':company_id', $companyId);
        $query->execute();
        $result = $query->fetch(PDO::FETCH_OBJ);
        return $result;
    }
}

MySQL Tables
companies
id     |     name   |    email   |   company_type_id

company_type
id | company_type

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lavrov95, 2018-02-23
@Lavrov95

SELECT companies.id, company_type.company_type, companies.email, companies.name, companies.company_type_id
            FROM companies
            INNER JOIN company_type
            ON companies.company_type_id=company_type.id WHERE companies.id = :company_id;

O
Oleg Lysenko, 2018-02-22
@oleglysenko

ON company_type.id = companies.company_type_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question