Php MYSQL query several tables

Asked

Viewed 1,762 times

-1

Good, I’m having difficulty showing the data of a database using 3 tables.

<?php
include("conectar.php");

$quantidade = 1;
$pagina = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1;
$inicio = ($quantidade * $pagina)-$quantidade;  
$sql = "Select * From tb_trabalhador order by id asc LIMIT $inicio, $quantidade";
$qr = mysql_query($sql) or die (mysql_error());
    while($exibe = mysql_fetch_array($qr)){

I have 2 more tables to display the data there. I did the test with only two tables and also does not show me the entered data

 SELECT * FROM tb_trabalhador
 INNER JOIN tb_detalhe_trabalhador ON Tb_trabalhador.id     =tb_detalhe_trabalhador.tb_trabalhador_id;

I tried Left and Right and displays the data from the respective tables. I wonder if you can help me with this problem?

  • this page has some examples http://blog.thiagobelem.net/relations-tabelas-no-mysql/

  • 2

    As much as some have understood your problem, it is unclear what you are asking. Leave on record what you are needing.

  • I’ve already made changes and I hope it’s clearer now my doubt

  • @user3253195 As far as you can understand, you want to make a Join with the 3 tables. The problem would not be the PHP code. I believe that the question as a whole is not very clear from the words used, such as the term "call tables". Also, you say that the result of your attempt was 0 (zero), but what does that mean? Zero records? Sum equal to zero? Where’s your Join attempt? I think your question was closed because people couldn’t interpret what you said. If I understand correctly, I suggest editing again, including the title, for something related to SQL and Joins.

3 answers

3

Use a Join to join the tables and access all the fields you need:

SELECT * FROM tb_trabalhador as t
INNER JOIN tb_equipamentos as e ON t.id = e.trabalhador_id
INNER JOIN tb_detalhe_trabalhador as d ON t.id = d.tb_trabalhador_id

instead of the asterisk * put the required fields in the format tabela.nome_do_campo or alias.nome_do_campo

Ex:

SELECT e.marca, e.modelo, t.nome, t.matricula, d.outro_campo FROM ....
INNER ....

Related content: difference between INNER X OUTER

  • does not need the Inner Join

  • need what in place? did not understand your comment

  • Making one select to make the other is not feasible when you can use the @Silvio Andorinha joints. The bank will work much faster with the join than separate equal in the link you posted to him in the comment. However it is a way to do too, is at his discretion.

  • @lionbtt see my answer http://answall.com/questions/6706/php-query-varias-tabelas/6715#6715

  • only a question. ?

  • AS is to specify an alias is not required in some banks serves only to highlight the abbreviation.

  • I tested the INNER JOIN and the result is blank. LEFT, Right only shows me one of the tables. Is there any other solution?

Show 2 more comments

1

To illustrate the use of JOIN as @perdeu falou, follows

<?php
include("conectar.php");

$quantidade = 1;
$pagina = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1;
$inicio = ($quantidade * $pagina)-$quantidade;  
$sql = "SELECT * FROM tb_trabalhador as t ".
        " INNER JOIN tb_equipamentos as e ON t.id = e.trabalhador_id "
        " INNER JOIN tb_detalhe_trabalhador as d ON t.id = d.tb_trabalhador_id order by id asc LIMIT $inicio, $quantidade";
$qr = mysql_query($sql) or die (mysql_error());
  while($exibe = mysql_fetch_array($qr)){

If the table tp_equipamento is not always mandatory, that is, it may not have related to tp_trabalhador, you will have to use the LEFT JOIN

  • does not need the Inner Join

  • only a question. After the "AS" the letters are corresponded to what?

0

Very simple way using the junction in the clause WHERE

<?php
include("conectar.php");

$quantidade = 1;
$pagina = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1;
$inicio = ($quantidade * $pagina)-$quantidade;  
$sql = "SELECT * FROM tb_trabalhador as t, tb_equipamentos e , tb_detalhe_trabalhador d ".
       " WHERE t.id = e.trabalhador_id AND t.id = d.tb_trabalhador_id ".
       " order by id asc LIMIT $inicio, $quantidade";
$qr = mysql_query($sql) or die (mysql_error());
  while($exibe = mysql_fetch_array($qr)){
  • @lost look here without the Inner Join

  • Okay, another way to do it.. However, you could post the reply instead of commenting on it.

  • 3

    This way of joining the tables was done in old databases (ansi-89 syntax) which uses the join is newer (ansi-92 syntax), I believe leave the WHERE Only for filters is more legible than using it to make junctions and filters.

Browser other questions tagged

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