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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:32:13+00:00 2026-05-21T02:32:13+00:00

Update Here is the ddl of the two tables that the CASE statement uses

  • 0

Update
Here is the ddl of the two tables that the CASE statement uses

CREATE TABLE product_option (
merchant_id                 smallint unsigned NOT NULL,
product_option_id           smallint unsigned NOT NULL AUTO_INCREMENT,
product_option_name         varchar(255) NOT NULL DEFAULT '',
product_id                  int unsigned NOT NULL DEFAULT 0,    /* Option may be associated with a product  */
package_id                  int unsigned NOT NULL DEFAULT 0,    /* or all products with this package    */
PRIMARY KEY pk_product_option (merchant_id,product_option_id),

) TYPE=MyISAM PACK_KEYS=1 COMMENT='Options';

CREATE TABLE package_2_product (
merchant_id     smallint unsigned NOT NULL,
package_id      int unsigned NOT NULL,
product_id      int unsigned NOT NULL,

PRIMARY KEY pk_package_2_product (merchant_id,package_id,product_id)

) TYPE=MyISAM PACK_KEYS=1 COMMENT='Link product to package';

I have a query…

SELECT SQL_CALC_FOUND_ROWS 
  p.*,
  CASE p.in_stock_msg  WHEN '' THEN 'In stock' ELSE p.in_stock_msg  END AS in_stock_msg, 
  CASE p.out_stock_msg WHEN '' THEN ''         ELSE p.out_stock_msg END AS out_stock_msg,
  CASE WHEN EXISTS (
    SELECT product_option_id 
      FROM product_option
     WHERE merchant_id = 116 AND product_id = p.product_id
     UNION
    SELECT product_id FROM package_2_product
     WHERE merchant_id = 116 AND product_id = p.product_id
  ) THEN 1 ELSE 0 END AS options_exist,
  i.thumbnail,i.thumbnail_width,i.thumbnail_height,
  i.title AS thumbnail_title, i.alt AS thumbnail_alt
FROM 
  product p 
  INNER JOIN category_2_product c2p ON p.merchant_id=c2p.merchant_id 
                                       AND p.product_id=c2p.product_id 
                                       AND c2p.category_id = 84
  LEFT  JOIN product_image        i ON p.merchant_id = i.merchant_id
                                       AND p.product_id = i.product_id
                                       AND i.is_default = 1 
WHERE 
  p.merchant_id = 116
  AND FIND_IN_SET('live',p.param) > 0
  AND FIND_IN_SET('wholesale-only',p.param) = 0
ORDER BY
  p.rank, p.product_name
LIMIT 0, 50;

The pertinent part being the CASE WHEN EXISTS…
is causing the query to take multiple seconds to run. Without the sub-select it finishes in a tenth of a second.

I’m wondering if there is a way to move this type of case, sub-select into an inner join to get the same data, and not have the sub-select run for every row that gets returned.

Attempting the below query with a tweak to the Coalesce line and the LEFT JOIN (SELECT…) p1

I received this error…

Error Code: 1054
Unknown column ‘p1.product_id’ in ‘on clause’

Here is the query…

SELECT SQL_CALC_FOUND_ROWS 
  p.*,
  CASE p.in_stock_msg  WHEN '' THEN 'In stock' ELSE p.in_stock_msg  END AS in_stock_msg,  
  CASE p.out_stock_msg WHEN '' THEN ''         ELSE p.out_stock_msg END AS out_stock_msg,
  COALESCE(p1.product_option_id, p2.product_id) IS NOT NULL AS options_exist,
  i.thumbnail,i.thumbnail_width,i.thumbnail_height,
  i.title AS thumbnail_title, i.alt AS thumbnail_alt
FROM 
  product p 
  INNER JOIN category_2_product c2p ON p.merchant_id=c2p.merchant_id 
                                       AND p.product_id=c2p.product_id 
                                       AND c2p.category_id = 84
  LEFT  JOIN product_image        i ON p.merchant_id = i.merchant_id
                                       AND p.product_id = i.product_id
                                       AND i.is_default = 1 
  LEFT JOIN (
   SELECT product_option_id
     FROM product_option
    WHERE merchant_id = 116
    GROUP BY product_id) p1 on p1.product_id = p.product_id
   LEFT JOIN (
    SELECT product_id
    FROM package_2_product
   WHERE merchant_id = 116
    GROUP BY product_id) p2 on p1.product_id is null and p2.product_id = p.product_id 
WHERE 
  p.merchant_id = 116
  AND FIND_IN_SET('live',p.param) > 0
  AND FIND_IN_SET('wholesale-only',p.param) = 0
ORDER BY
  p.rank, p.product_name
  • 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-21T02:32:14+00:00Added an answer on May 21, 2026 at 2:32 am
    SELECT SQL_CALC_FOUND_ROWS 
      p.*,
      CASE p.in_stock_msg  WHEN '' THEN 'In stock' ELSE p.in_stock_msg  END AS in_stock_msg, 
      CASE p.out_stock_msg WHEN '' THEN ''         ELSE p.out_stock_msg END AS out_stock_msg,
      COALESCE(p1.product_id, p2.product_id) IS NOT NULL AS options_exist,
      i.thumbnail,i.thumbnail_width,i.thumbnail_height,
      i.title AS thumbnail_title, i.alt AS thumbnail_alt
    FROM 
      product p 
      INNER JOIN category_2_product c2p ON p.merchant_id=c2p.merchant_id 
                                           AND p.product_id=c2p.product_id 
                                           AND c2p.category_id = 84
      LEFT  JOIN product_image        i ON p.merchant_id = i.merchant_id
                                           AND p.product_id = i.product_id
                                           AND i.is_default = 1 
      LEFT JOIN (
        SELECT product_id
          FROM product_option
         WHERE merchant_id = 116
         GROUP BY product_id) p1 on p1.product_id = p.product_id
      LEFT JOIN (
        SELECT product_id
          FROM package_2_product
         WHERE merchant_id = 116
         GROUP BY product_id) p2 on p1.product_id is null and p2.product_id = p.product_id
    
    WHERE 
      p.merchant_id = 116
      AND FIND_IN_SET('live',p.param) > 0
      AND FIND_IN_SET('wholesale-only',p.param) = 0
    ORDER BY
      p.rank, p.product_name
    

    Notes:

    • on p1.product_id is null is added to the 2nd left join to prevent it being executed when p1 already has a result.
    • The group by clauses prevent the LEFT JOINs from expanding the result set via cartesian product

    This DDL will create a table with enough fields to show the query working without errors.

    create table product_option(merchant_id int, product_id int);
    create table package_2_product(merchant_id int, product_id int);
    create table category_2_product(merchant_id int, product_id int, category_id int);
    create table product_image(merchant_id int, product_id int, is_default int,
        thumbnail int, thumbnail_width int, thumbnail_height int, title int, alt int);
    create table product(merchant_id int, product_id int, in_stock_msg int,
        out_stock_msg int, param int, rank int, product_name int);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: So pretty much everyone here has told me that I just need to
Replaces Question: Update multiple rows into SQL table Here's a Code Snippet to update
Update Luckily, the code that WebKit uses to make this control draws onto a
###########Update Here is the original code that worked in ie7 but not in 9.
update Here is the situation: I'm working on a website that has no physical
Update: here is a follow up question with colspans . I have a table
* UPDATE * Here is what I found. Whenever I had that function in
Update I summarized the question and its answers here My objective is to detect
Update In the wiki spirit of StackOverflow, here's an update: I spiked Joe White's
Update: This question was an epic failure, but here's the working solution. It's based

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.