Join a lot of selects into one

Asked

Viewed 82 times

0

I have 4 selects to do that I later put in the separate combobox only I want to put everything in one has like?

$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();

Kind of

$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)

  • Dude, you’re gonna be looking for Ner Join, left Join, right Join and stuff like

  • You want a select for each table and you want it to return the data of all or you want to take specific data that has relation in the other table?

  • And when you spin ->execute(), what will be returned? What if the amount of rows returned by each select is not the same?

2 answers

1

For the power to internalize, there is a simpler way to execute all these sql in a simpler way without using Pdo but 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 you have shown example, it will be 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 linked via FK, then the ideal will be to use INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, ETC.

There is an image with some examples.

inserir a descrição da imagem aqui

I hope I’ve helped.

0

BUDDY RS COME ON!

Even correct would be your tables having relationships with each other... However I believe you have not thought about it before, but I will try to help you...

Ideally you create a CLASS WORK as an example:

class Funcionario{

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

Create a Functionclass, implement 4 methods corresponding to its 4 SELECTS and with the returned data popularize a FUNCTION OBJECT

Browser other questions tagged

You are not signed in. Login or sign up in order to post.