I have two entry and purchase tables, each relating to your detail: entry_interior and detail_buying , which I want to get the last cost of a product which will be delimited by the most recent date, either the entry or the purchase.
I have it solved partially using UNION but it throws me two results. I would like to know how I can select one nothing.
This is the query:
SELECT IFNULL(
(SELECT detalle_entrada.costo FROM producto
INNER JOIN detalle_entrada ON detalle_entrada.idproducto = producto.idproducto
INNER JOIN entrada ON entrada.identrada = detalle_entrada.identrada
WHERE producto.idproducto = 153
ORDER BY entrada.fecha_entrada DESC
LIMIT 1),
0.0) as ultimo_costo
UNION
SELECT IFNULL(
(SELECT compra_detalle.precio_unitario FROM producto
INNER JOIN compra_detalle ON compra_detalle.idproducto = producto.idproducto
INNER JOIN compra ON compra.idcompra = compra.idcompra
WHERE producto.idproducto = 153
ORDER BY compra.fecha_compra DESC
LIMIT 1),
0.0) as ultimo_costo