Bring an id from another sql table

0

I am working with php and I need to point to the id of a detail table, the problem is that I show information from the main table but my button must point to the table information here my problem:

The id of the main table is 168 but in my detail button it must point to my id 85 that contains the detail of that table.

my detail table:

How would the query be to bring that id of my detail table to the main table?

Tables:

Entrada: 
id (relacion)
proveedor 
cajas
peso
hora

Detalle:
id
fecha
hora
identrada (relacion)
...

The information in table Entrada is displayed but the link points to the id of the detail. Eye! It does not always have detail so if you are going to bring the id of the detail, show it in 0, can you?

    
asked by DoubleM 20.04.2018 в 23:25
source

2 answers

1

Ok ... As far as I can see, it would be more so.

SELECT entradas.id as entrada_id, entradas.peso, entradas.cajas, entradas.hora, detalles.id as detalle_id
FROM entradas, detalles 
WHERE entradas.id = detalles.identrada 

It would be good for this type of questions to explain how your database is modeled so that the community can give you better help

    
answered by 20.04.2018 в 23:38
0

Try this query:

SELECT 
    e.proveedor, 
    e.cajas, 
    e.peso, 
    e.hora, 
    IFNULL(d.id,0) id_detalle 
FROM entrada e 
LEFT JOIN detalle d ON e.id=d.id 
WHERE e.id=168;

I explain:

Since LEFT JOIN will indicate that the id field in detalle will be NULL when there are no coincidences, the use of IFNULL will allow replacing it in that case by the value 0 . That column will appear in the results with the alias id_detalle .

    
answered by 21.04.2018 в 01:14