Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

360
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda