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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:50:43+00:00 2026-06-09T17:50:43+00:00

This is a continuation of my previous question here . In the following example:

  • 0

This is a continuation of my previous question here.

In the following example:

id       PRODUCT ID    COLOUR
1        1001          GREEN
2        1002          GREEN
3        1002          RED
4        1003          RED

Given a product ID, I want to retrieve only one record – that with GREEN colour, if one exists, or the RED one otherwise. It sounds like I need to employ DISTINCT somehow, but I don’t know how to supply the priority rule.

Pretty basic I’m sure, but my SQL skills are more than rusty..

Edit: Thank you everybody. One more question please: how can this be made to work with multiple records, ie. if the WHERE clause returns more than just one record? The LIMIT 1 would limit across the entire set, while what I’d want would be to limit just within each product.

For example, if I had something like SELECT * FROM table WHERE productID LIKE “1%” … how can I retrieve each unique product, but still respecting the colour priority (GREEN>RED)?

  • 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-06-09T17:50:45+00:00Added an answer on June 9, 2026 at 5:50 pm
    SELECT * 
    FROM yourtable
    WHERE ProductID = (your id)
    ORDER BY colour 
    LIMIT 1
    

    (Green will come before Red, you see. The LIMIT clause returns only one record)

    For your subsequent edit, you can do this

    select yourtable.*
    from
        yourtable
        inner join 
        (select productid, min(colour) mincolour 
        from yourtable
        where productid like '10%'
        group by productid) v
        on yourtable.productid=v.productid
        and yourtable.colour=v.mincolour    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is a continuation of my previous question here zend models architecture (big
This question is in continuation to my previous post located here . Since there
This is a continuation of my previous question .NET regex engine returns no matches
This question is in continuation to my previous question . I am trying to
This is a continuation of this question from yesterday . Here are my three
This is a continuation of my previous question which I posted when I wasn't
This question is a continuation of a previous thread to compare two lists with
This is in continuation to my previous question which was closed cause of similarity
This question is in continuation to my previous question, in which I asked about
This is a continuation from a previous stackoverflow question. I've renamed some variables so

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.