Daniel B
Daniel B

Reputation: 137

bring the last 10 orders whitout repeating

I have the following query

SELECT a.id, a.fecha, a.ser, a.numero, c.nombre_apellido, a.estado, a.tipo, a.articulo, a.precio_asignado, a.retirada, a.pronta, a.precio, a.confirmada, d.marca, a.modelo, a.fecha_prometido, a.fecha_asignado, a.presupuesto, a.cant_llamados
FROM (
(
(
ordenes_servicio_bitacora b
LEFT JOIN ordenes_reparaciones a ON b.id_orden = a.id
)
LEFT JOIN clientes c ON a.cliente_id = c.id
)
LEFT JOIN marcas d ON a.marca_id = d.id
)
ORDER BY b.id_bitacora DESC 
LIMIT 10

and it brings me this enter image description here

as you see the id column of the order is repeated because of the cross with bitacora table i need not to be repeated, any ideas? Thanks in advance.

Upvotes: 0

Views: 54

Answers (2)

Daniel B
Daniel B

Reputation: 137

    SELECT a.id, a.fecha, a.ser, a.numero, c.nombre_apellido, a.estado, a.tipo, a.articulo, a.precio_asignado, a.retirada, a.pronta, a.precio, a.confirmada, d.marca, a.modelo, a.fecha_prometido, a.fecha_asignado, a.presupuesto, a.cant_llamados
FROM (
(
(
(SELECT DISTINCT id_orden
FROM  `ordenes_servicio_bitacora` 
ORDER BY id_bitacora DESC 
LIMIT 10) b
LEFT JOIN ordenes_reparaciones a ON b.id_orden = a.id
)
LEFT JOIN clientes c ON a.cliente_id = c.id
)
LEFT JOIN marcas d ON a.marca_id = d.id
)

i just made a subquery and the job is done, thanks by the answers and comments :)

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79949

DISTINCT should do the job in your case, as all the columns' data are repeated for the row not just the id column:

SELECT DISTINCT 
  a.id, a.fecha, a.ser, a.numero, 
  c.nombre_apellido, a.estado, 
  a.tipo, a.articulo, a.precio_asignado, 
  a.retirada, a.pronta, a.precio, 
  a.confirmada, d.marca, a.modelo, 
  a.fecha_prometido, a.fecha_asignado, 
  a.presupuesto, a.cant_llamados
FROM ordenes_servicio_bitacora b
LEFT JOIN ordenes_reparaciones a ON b.id_orden   = a.id
LEFT JOIN clientes c             ON a.cliente_id = c.id
LEFT JOIN marcas d               ON a.marca_id   = d.id 
ORDER BY b.id_bitacora DESC   
LIMIT 10

Upvotes: 1

Related Questions