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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:11:37+00:00 2026-06-17T22:11:37+00:00

I have three tables, products , variants , and data . Each product has

  • 0

I have three tables, products, variants, and data.

Each product has it’s own productcode. Products can have any number of variants including none. Variants have their own code variantcode. Products have a productid column, and each variant has a related productid column.

I want a list of all the codes, but I only care about the productcode if a product has no variants. Otherwise I want the variantcode. I wrote this query to get this list:

SELECT IFNULL(variants.variantcode, products.productcode) AS code 
FROM products 
LEFT OUTER JOIN variants ON (products.productid = variants.productid) 
ORDER BY code

This query works as I expected.

The data table contains extra data for each code. I want to join this data onto this list. I tried this query:

SELECT IFNULL(variants.variantcode, products.productcode) AS code 
FROM products 
LEFT OUTER JOIN variants ON (products.productid = variants.productid)
LEFT OUTER JOIN data ON (data.partno = code) 
ORDER BY code

But I get an error “Unknown column ‘code’ in ‘on clause'”. I assumed this had something to do with code being a generated value, so I then tried this query:

SELECT IFNULL(variants.variantcode, products.productcode) AS code 
FROM products 
LEFT OUTER JOIN variants ON (products.productid = variants.productid) 
LEFT OUTER JOIN data ON (data.partno = IFNULL(variants.variantcode, products.productcode)) 
ORDER BY code

This query worked, but took a long time (~20 seconds vs <1 second for the first query). Is the IFNULL in the ON clause the problem? How can I speed it up?

  • 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-17T22:11:38+00:00Added an answer on June 17, 2026 at 10:11 pm

    Try this:

     select product_variants.code from
    
     (SELECT IFNULL(variants.variantcode, products.productcode) AS code 
     FROM products 
     LEFT OUTER JOIN variants ON (products.productid = variants.productid) 
     ORDER BY code) 
    
     as product_variants
    
     LEFT OUTER JOIN data ON (data.partno = product_variants.code) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables products, sales and costs. The sales and products table each
We have three tables to hold our products and keywords: Product {int ID, string
I have three tables. I have to retrieve the data using Linq statement. My
I have three tables of data: table: cars [10,000 rows] table: planes [2,000 rows]
I have this three tables: products TABLE: id:integer name:string features TABLE: id:integer name:string features_products
I have three tables, Customers, Sales and Products. Sales links a CustomerID with a
I have three tables Prospect -- holds prospect information id name projectID Sample data
I have a Doctrine2 project with three tables: Product , Category and Rating .
I'm doing a webshop, and every product can have several variants. All variants are
I have three tables, products , shops , and sex . I wish to

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.