Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

366
Vistas
Laravel select unique products with lowest price from one table

Good day, I'm trying to get unique products with lowest prices. I'm having a products table like this: enter image description here

I would like to get a list of products with all columns. Now there are some products that have more than one supplier in that case I want to grab the product with the lowest cost_price.

So far I have tried this

$products = DB::table('products')
        ->select('identifier')
        ->selectRaw('MIN(cost_price) as cost_price')
        ->where('stock', '>', 0)
        ->groupBy('identifier')
        ->orderBy('cost_price', 'asc')
        ->distinct()->get();

This query returns me the correct results but I cant add more columns every time I add a column for example stock in select I need to add as well in GroupBy and then I'm just getting all products.

How to do it? Thank you for reading.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need greatest-n-per-group solution/approach for this problem.

The query;

SELECT products.*
FROM products
         INNER JOIN (SELECT identifier, MIN(cost_price) AS minPrice
                     FROM products
                     WHERE stock > 0
                     GROUP BY identifier) AS sub
             ON sub.minPrice = products.cost_price and sub.identifier = products.identifier;

The query builder version;

$sub = DB::table('products')
    ->where('stock', '>', DB::raw(0))
    ->groupBy('identifier')
    ->select('identifier', DB::raw('min(cost_price) as minPrice'));

return DB::table('products')
    ->join(DB::raw('(' . $sub->toSql() . ') as sub'), function ($join) {
        $join->on('sub.minPrice', '=', 'products.cost_price');
        $join->on('sub.identifier', '=', 'products.identifier');
    })
    ->get(['products.*']);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda