LEFT JOIN with three tables gone wrong

Asked

Viewed 91 times

-1

because this query is returning me all the elements of the table offer_pro I want and only the table id offers?

is not taking the elements title, description, value, user_of, categ, location, fav, table offers

$cmd="SELECT 
ofertas.id, 
ofertas.titulo, 
ofertas.descricao,  
ofertas.valor, 
ofertas.user_of, 
ofertas.categ, 
ofertas.local, 
ofertas.fav, 

favoritos.id_oferta,

ofertas_pro.id, 
ofertas_pro.titulo, 
ofertas_pro.descricao,  
ofertas_pro.valor, 
ofertas_pro.user_of, 
ofertas_pro.categ, 
ofertas_pro.local, 
ofertas_pro.fav

FROM favoritos 

LEFT JOIN ofertas ON (ofertas.id=favoritos.id_oferta) 
LEFT JOIN ofertas_pro ON (ofertas_pro.id=favoritos.id_oferta)";

I tried removing the reserved variable "location" but the problem continues. I only get the field id from the offers. It this way does not take the rest of the data The complete code is this:

$cmd="SELECT 
    ofertas.id, 
    ofertas.titulo, 
    ofertas.descricao,  
    ofertas.valor, 
    ofertas.user_of, 
    ofertas.categ, 
    ofertas.local, 
    ofertas.fav, 

    favoritos.id_oferta,

    ofertas_pro.id, 
    ofertas_pro.titulo, 
    ofertas_pro.descricao,  
    ofertas_pro.valor, 
    ofertas_pro.user_of, 
    ofertas_pro.categ, 
    ofertas_pro.local, 
    ofertas_pro.fav

    FROM favoritos 

    LEFT JOIN ofertas ON (ofertas.id=favoritos.id_oferta) 
    LEFT JOIN ofertas_pro ON (ofertas_pro.id=favoritos.id_oferta)";
    //ORDER BY f.id_user='$login_session'";*/???



        $produtos = mysql_query($cmd);
        $total = mysql_num_rows($produtos);

    //exibe os produtos 
            echo "<table style= width:auto>";

            echo "<tr>";
            echo "<th>ID</th>";
            echo "<th>Empresa</th>";
            echo "<th>Categoria</th>";
            echo "<th>Serviço</th>";
            echo "<th>Descrição</th>";
            echo "<th>Pagamento</th>";
            echo "<th>Distrito</th>";
            echo "<th>Ações</th>";
            echo "<th>Avaliar</th>";
            echo "<th>Total</th>";
            echo "</tr>";
        while ($produto = mysql_fetch_array($produtos)) {


            echo "<tr>";
            echo "<td>".$produto['id_oferta']."</td>";
            echo "<td>autor:".$produto['user_of'] . "</td>";
            echo "<td>".$produto['categ'] . "</td>";
            echo "<td>".$produto['titulo'] . "</td>";
            echo "<td>".$produto['descricao'] . "</td>";
            echo "<td>".$produto['valor'] . "</td>";
            echo "<td>".$produto['local'] . "</td>";
            echo "<td><a href=aceita.php?id=".$produto['id'].">Aceitar</a></td>";
            echo "<td><a  href=fav.php?id=".$produto['id']."><img src='img/fav.png' height='24' width='24'></a></td>";
            echo "<td>".$produto['fav'] . "</td>";





            echo "</tr>";

        }
            echo "</table>";

    ?>
  • It is very strange LEFT JOIN with two other identical tables. If you run this query with INNER JOIN, it would give the same result if it is as you say (that the corresponding rows in the tables exist), which happens?

  • I don’t get anything from the comic.. = / any idea?

  • Would have to be missing the id in the table offers, which exists in the other two (offers_pro and favorites).

  • 12 If I replace the offers_pro by offers and where are the offers replace by offers_pro, it runs the offers and leaves the offers_pro.

  • offers_pro.local the word "local" is a reserved SQL variable.

  • If you have a new question, ask it by clicking the button Ask question. Include a link to this question if it helps provide context.

  • I simulated the tables and the command here and it worked (I created only the key and one more field, not all, but it would be enough to catch some problem in SQL).

  • What database is this in? I used Mysql to test.

  • 1

    I recommend replacing mysql_* with mysqli_*, as it will soon be discontinued.

Show 4 more comments

2 answers

0

This is because the fields have the same name, I say this at first glance. Try assigning names to fields that have equal names, as follows:

SELECT 
ofertas.id AS oferta_id, 
ofertas.titulo AS oferta_titulo, 
ofertas.descricao AS oferta_descricao,  
ofertas.valor AS oferta_valor, 
ofertas.user_of AS oferta_user_of, 
ofertas.categ AS oferta_categ, 
ofertas.local AS oferta_local, 
ofertas.fav AS oferta_fav, 

favoritos.id_oferta,

ofertas_pro.id AS oferta_pro_id, 
ofertas_pro.titulo AS oferta_pro_titulo, 
ofertas_pro.descricao AS oferta_pro_descricao,  
ofertas_pro.valor AS oferta_pro_valor, 
ofertas_pro.user_of AS oferta_pro_user_of, 
ofertas_pro.categ AS oferta_pro_categ, 
ofertas_pro.local AS oferta_pro_local, 
ofertas_pro.fav AS oferta_pro_fav

FROM favoritos 

LEFT JOIN ofertas ON (ofertas.id=favoritos.id_oferta) 
LEFT JOIN ofertas_pro ON (ofertas_pro.id=favoritos.id_oferta)

Although I think this is the right solution to the problem, strongly advise to change the table structure since this seems to me very wrong. The normal would be to have the table ofertas with a field ofertas.is_pro that if it had the value 1 would be a pro offer, if it had the value 0 would be a normal offer.

-1

Try this, and see if it works, I didn’t test it, glue the structure of the tables so I can test it here:

SELECT TOTAL.* FROM (
SELECT  ofertas.id, 
        ofertas.titulo, 
        ofertas.descricao,  
        ofertas.valor, 
        ofertas.user_of, 
        ofertas.categ, 
        ofertas.local_of, 
        ofertas.fav
  FROM ofertas
UNION ALL
SELECT ofertas_pro.id, 
ofertas_pro.titulo, 
ofertas_pro.descricao,  
ofertas_pro.valor, 
ofertas_pro.user_of, 
ofertas_pro.categ, 
ofertas_pro.local_of, 
ofertas_pro.fav
  FROM ofertas_pro 
  WHERE ofertas_pro.id IN (SELECT ofertas_pro.id from ofertas,
                           SELECT favoritos.id_oferta from favoritos) 
) TOTAL
;
  • 1

    I don’t know why you were negative, but like I said, put the structure of the tables, maybe I can help you.

Browser other questions tagged

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