Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3593566
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:35:39+00:00 2026-05-18T19:35:39+00:00

How does one filter a list of records to remove those that have some

  • 0

How does one filter a list of records to remove those that have some identical fields, based on selecting the one with the minimum value in another field? Note that it’s not sufficient to just get the minimum value… I need to have other fields from the same record.

I have a table of “products”, and I am trying to add the ability to apply a coupon code. Because of how the invoices are generated, selling a product at a different cost is considered a different product. In the database you might see this:

Product ID, Product Cost, Product Name, Coupon Code
    1,          20,          Product1,     null
    2,          10,          Product1,    COUPON1
    3,          40,          Product2,     null

I have a query that selects a list of all products available now (based on other criteria; I’m simplifying this a lot). The problem is that, for the above case, my query returns:

  1 - Product1 for $20
  2 - Product1 for $10
  3 - Product2 for $40

This gets shown to the customer (assuming they’ve entered the coupon code), and it’s obviously bad form to show a customer the same product for two prices. What I want is:

  2 - Product1 for $10
  3 - Product2 for $40

i.e., showing the lowest-costing version of each product.

I need a solution that will work for MySQL, but the preferred solution would be standard SQL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:35:40+00:00Added an answer on May 18, 2026 at 7:35 pm

    Try this:

    SELECT T2.* 
    FROM
    (
        SELECT `Product Name` AS name, MIN(`Product Cost`) AS cost
        FROM products
        GROUP BY `Product Name`
    ) T1
    JOIN products T2
    ON T1.name = T2.`Product Name`
    AND T1.cost = T2.`Product Cost`
    

    To get the output exactly as you described as a string replace the first line with:

    SELECT CONCAT(`Product ID`, ' - ', T1.name, ' for $', T1.cost)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list that the user can filter in several ways. two of
How does one go about authoring a Regular Expression that matches against all strings
How does one invoke a groovy method that prints to stdout, appending the output
How does one get a list of running applications as shown in the "Applications"
I have a simple form that I use to list the names of all
In jQuery, filter() reduces your result to those elements that fulfill a certain condition.
I feel like I am writing something that should already exist. Does Django have
I have a simple app that consists of: Model Items Filter criteria applied to
Does any one know of a function/idiom (in any language) that takes a set
I know that you can't call a method using a filter because it does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.