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 7680797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:11:53+00:00 2026-05-31T18:11:53+00:00

I’m really struggling with this now, been on it for ages and asked two

  • 0

I’m really struggling with this now, been on it for ages and asked two questions on here already!

So, here’s a hopefully more defined question.

I have a product database with several thousand products.

Products can be grouped by item so as not to display in my search results multiple items that are essentially the same but with different finishes.

So, a simple select query would look like:

SELECT * FROM products GROUP BY item

This would give me all the products showing just one per item (the idea being that the viewing customer clicks something else to show all the finish options)

I could get my finish options using something like

SELECT *, GROUP_CONCAT(finishes) FROM products GROUP BY item

BUt… and here’s where I have problems

In my search results pages, the “random” order of the grouping is no good to me. I need to be able to have the displayed item from the group clause depending on the following condtions

- If there is no stock of any finish, then display the cheapest option
- If there is stock, then display the cheapest item which is in stock

Getting just an ID is no good, I need the full record, so somehow what I need is a condition statement to determine the order in which the grouped records are presented to ensure that the item that I get from the recordset meet the criteria above.

I simply cannot work out how to do this, I’ve tried millions of different INNER JOIN and `CASE scenarios, but just can’t get it to work.

Here’s where I have got to this morning:

SELECT * FROM products prod INNER JOIN 
    (SELECT id, item, MIN(price) AS minPrice, totalStock, MAX(totalStock) AS maxStock,
    sum(totalStock) as sumTotalStock FROM products WHERE " & whereClause & "
GROUP BY item ORDER BY price) sortedProd ON 
    CASE WHEN sortedProd.sumTotalStock > 0 THEN " ' if there is some of this item in stock
        CASE WHEN sortedProd.minPrice = prod.price AND sortedProd.item = prod.item AND sortedProd.totalStock > 0 THEN  ' check if the item in stock is the cheapest
             sortedProd.minPrice = prod.price AND sortedProd.item = prod.item " ' if the cheapest one is ins tock, display it
        ELSE sortedProd.totalStock > 0  AND sortedProd.item = prod.item END" 'if not display whichever is in stock
    ELSE sortedProd.minPrice = prod.price AND sortedProd.item = prod.item END ' otherwise display the cheapest
GROUP BY prod.item ORDER BY prod.price DESC 

This is giving me results, but form reason some items are being excluded from the results, and the sorting order is still not correct.

To answer @DRapp below, here’s some basic sample data

item      |    price     |     finish         | totalStock

item 1    |     £1       |       red          |     2
item 1    |     £1       |       blue         |     1
item 1    |     £1.50    |       blue & red   |     1

item 2    |     £2       |       red          |     0
item 2    |     £3       |       blue         |     1

item 3    |     £3.50    |       blue & red   |     1
item 3    |     £1       |       red          |     0
item 3    |     £2       |       blue         |     1

item 4    |     £4.50    |       blue & red   |     1

item 5    |     £1.50    |       blue         |     0
item 5    |     £2.50    |       green        |     0
item 5    |     £0.50    |       orange       |     0

The result I’d like to see is:

  • item 1, red (sumTotalStock = 4) – ‘red and blue are both cheapest & in stock. Either result shown is fine, but to be 100% perfect, show red as the stock is greatest
  • item 2, blue (sumTotalStock = 1) -‘ blue is the only finish in stock
  • item 3, blue (sumTotalStock = 2) – ‘display blue as it’s cheapest in stock
  • item 4, blue & red (sumTotalStock = 1) -‘there are no finish options, so just show this one regardless of stock/price
  • item 5, orange (sumTotalStock = 0) – ‘none in stock, but show orange as it’s cheapest.

Obviously there is more data to actually display than just finish and price, product description etc also, which may vary depending on finish, hence I do need to get the full record, but this data example should explain the principle.

  • 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-31T18:11:54+00:00Added an answer on May 31, 2026 at 6:11 pm

    This should do what you want using a “simple” left join and no nested selects; your sort criteria make the query look a bit bulky though 🙂

    SELECT p1.*
    FROM Products p1
    LEFT JOIN Products p2
      ON p1.item=p2.item AND
         (IF(p1.totalstock,1,0) < IF(p2.totalstock,1,0) OR
          (IF(p1.totalstock,1,0) = IF(p2.totalstock,1,0) AND
           p1.price > p2.price) OR
          (p1.price = p2.price AND 
           p1.totalstock < p2.totalstock) OR
          (p1.price = p2.price AND 
           p1.totalstock = p2.totalstock AND
           p1.id > p2.id))
    WHERE p2.id IS NULL
    

    Demo here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.