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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:26:17+00:00 2026-05-26T05:26:17+00:00

I am certain this is non-conventional, however I am trying to perform the following:

  • 0

I am certain this is non-conventional, however I am trying to perform the following:

I have the following Table:

products_id, company_id, products_name
1,           1,          shoes
1,           2,          mens shoes
2,           1,          pants

Now, what I want to do is select a particular products_name from this using a given products_id and company_id = ‘2’, however in all cases where it does not find a row with company_id = ‘2’ I want it to default to giving me the products_name for the same product_id with ANY company_id.

For instance, if I were to search for products_id = ‘2’, company_id = ‘2’ it should return products_name = ‘pants’. However, if I were to search products_id = ‘1’, company_id = ‘2’ it should return ‘mens shoes’. If I were to search products_id = ‘3’, company_id = anything – it should return an empty set.

I need to be able to do this in a single SQL statement, so no handling via PHP to confirm whether or not a row exists with say: products_id = ‘2’ company_id = ‘2’.

The actual application uses a far more complex table with multiple joins, however it’s this simple bit that I cannot figure out, so if anyone could help me with this hurdle it would be much appreciated.

  • 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-26T05:26:18+00:00Added an answer on May 26, 2026 at 5:26 am

    Here is a query that will return the company’s product, or any generic one if there isn’t a specific product for that company, across the entire product list. It can easily be filtered with an extra where products_id = ## at the end (or by putting a where filter inside the two inner sub-queries which might actually be more efficient):

    set @company_id = 2;
    
    select
        coalesce(p2.products_id, p1.products_id) as products_id,
        coalesce(p2.products_name, p1.products_name) as products_name
    from
        (select products_id, max(products_name) as products_name
            from products group by products_id) p1
    left join
        (select products_id, products_name
            from products where company_id = @company_id) p2
        on p1.products_id = p2.products_id
    

    Demo: http://sqlize.com/pkt568O0b5

    The basic idea behind my logic was this:

    1. Create a query that pulls in an arbitrary product name per product id – by just pulling the max of product name and grouping by id.
    2. Create a query that pulls the product name for the company being filtered for.
    3. Left join #2 with #1, so that I will have a list of products, plus the product name for the company if it is available. Because of the left join, all the products will still be returned, but for the products that don’t have a specific name for the given company, the company-specific list will be NULL.
    4. Select the product id and name from the given company-specific list, if available. If not available, then use the generic list which just pulls max of product name. This is handled through the coalesce function, which returns the first non-null argument in its list. So basically it checks the company-specific list first, and if nothing is available, uses the generic list instead.

    Hope this helps!

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

Sidebar

Related Questions

I have this simple (NON PRACTICAL) table STUDENTID SERVICE 1 a 1 b 1
I use the following bash script to copy only files of certain extension(in this
This is for certain a newbie mistake, I have googled for plenty of hours
I have this method which does certain checks and applies a color code on
I have this function which is supposed to set a certain time format to
I have symlinks to certain directories because the directories' names have non English characters
I have gotten this to work nicely, but when speed hits a certain speed
I am trying to set certain items in my gridview from clickable to non
This would have been a lot easier if not for certain situations. Sample data:
I have this block of html: <div> <p>First, nested paragraph</p> </div> <p>First, non-nested paragraph.</p>

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.