Join a bunch of selects in one

0

I have 4 selects to do that I then put in the separate combobox only I want to put everything together in a single has?

$sqlq = $pdo->prepare("SELECT * FROM tbl_tipo ORDER BY tipo ASC");
$sqlq ->execute();
$sqlqu = $pdo->prepare("SELECT cod_empresa,razao_social FROM tbl_empresa ORDER by razao_social ASC");
$sqlqu ->execute();
$sqlque = $pdo->prepare("SELECT * FROM tbl_pessoa_fisica ORDER BY nome ASC");
$sqlque ->execute();
$sqlquer = $pdo->prepare("SELECT cod_empregado,nome FROM tbl_empregado ORDER BY nome ASC");
$sqlque ->execute();

Type like this

$sqlquery = $pdo->prepare("SELECT tipo.*, empresa.*, pessoa.*, empregado.* FROM tbl_tipo tipo tbl_empresa empresa tbl_pessoafisica pessoa tbl_empregado empregado")

And then do that $linha->fetch(PDO::FETCH_ASSOC)

    
asked by anonymous 10.05.2017 / 15:57

2 answers

1

To be able to internalize, there is a simpler way to execute these sql all in a simpler way without using pdo but it will be at your discretion:

$sql = ("SELECT * FROM tbl_tipo ORDER BY tipo ASC"); 
$sql .=("SELECT cod_empresa,razao_social FROM tbl_empresa ORDER by razao_social ASC");
$sql .=("SELECT * FROM tbl_pessoa_fisica ORDER BY nome ASC");
$sql .=("SELECT cod_empregado,nome FROM tbl_empregado ORDER BY nome ASC"); 
 $result=mysqli_multi_query($conn, $sql);

If you want all the data as it has an example, it will look like this:

$sqlquery = $pdo->prepare("SELECT * FROM tbl_tipo tipo, tbl_empresa empresa, tbl_pessoafisica pessoa, tbl_empregado empregado")

If you want specific data where tables are connected through FK , then the ideal one is to use INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, ETC.

It has an image with some examples.

I hope I have helped.

    
10.05.2017 / 18:01
0

AMIGÃO RS WE GO THERE!

Correct even if your tables have relationships with each other ... But I believe that you have not thought about this before, but I will try to help you ...

The ideal is to create a FUNCTIONAL CLASS as an example:

class Funcionario{

  private $nome;
  private $tipo; 
  private $empresa;
  private $codEmpresa;
  private $razaoSocial; 
  //ENTRE OUTROS PARAMETROS
}

I would create a FunctionObject class, implement 4 methods corresponding to its 4 SELECTS, and with the data returned it popularizes an OBJECT Functional

    
10.05.2017 / 19:24