Displaying the Same Record Twice [duplicate]

0

This is my code that makes a table listing

<?php 
    $consulta = mysql_query("SELECT * FROM mesas INNER JOIN pedidos WHERE pedidos.mesa = mesas.id and conta = 0");
    if (mysql_num_rows($consulta)==true) {
       while($lnmesas = mysql_fetch_array($consulta)){ 
?>   

I put a request in the database and it inserts it with the name, size, value, produced = 0, account = 0, and the table number, so I made this inner join there because I just want it to display the table that contains a record in the database that the table column is = the table number in the table tables ...

But for example I launch two orders with the number of table 20 Then he shows me twice the table 20 if I post 4 orders on the table 15 he displays to me 4 times the table 15, and I wanted him to display only one of each.

I created SQL Fiddle to improve the explanation of the problem.

    
asked by anonymous 03.11.2015 / 19:03

1 answer

4

Expensive,

The querys below will return only the tables that exist in the orders table. Because the table requests contain more than one table, it is necessary to use distinct so that duplicate rows do not return.

select id
  from mesas
 where id in ( select distinct mesa
                 from pedidos
                where conta = 0 );

OU

select distinct a.id
  from mesas a,
       pedidos b
 where a.id = b.mesa
   and b.conta = 0;

Hug.

    
03.11.2015 / 22:46