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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:41:29+00:00 2026-05-23T06:41:29+00:00

Is it possible to rewrite this query without using a subquery? SELECT `products`.`id` AS

  • 0

Is it possible to rewrite this query without using a subquery?

SELECT `products`.`id` AS `id`,`products`.`parent_id` AS `parent_id`,`products`.`lft` AS `lft`,`products`.`rght` AS `rght`,`products`.`primary` AS `primary`,`products`.`friendly` AS `friendly`,`products`.`code` AS `code`,`products`.`title` AS `title`,`products`.`meta_description` AS `meta_description`,`products`.`meta_keywords` AS `meta_keywords`,`products`.`name` AS `name`,`products`.`description` AS `description`,`products`.`purchase_price` AS `purchase_price`,`products`.`sale_price` AS `sale_price`,`products`.`tax_code_id` AS `tax_code_id`,`products`.`colour_id` AS `colour_id`,`products`.`size_id` AS `size_id`,`products`.`num_sales` AS `num_sales`,`products`.`showcase` AS `showcase`,`products`.`showcase_image` AS `showcase_image`,`products`.`classic` AS `classic`,`products`.`new_release` AS `new_release`,`products`.`default_colour_id` AS `default_colour_id`,`products`.`deleted` AS `deleted`,`products`.`stock_level` AS `stock_level`,`products`.`hidden` AS `hidden`,`products`.`manufacturer_id` AS `manufacturer_id`,`products`.`created` AS `created`,`products`.`modified` AS `modified`
from `products`
where `products`.`id` NOT IN (
    select `parentproduct`.`id` 
        from `orders`
            join `baskets` on `orders`.`basket_id` = `baskets`.`id`
            join `basket_items` on `basket_items`.`basket_id` = `baskets`.`id` AND `basket_items`.`deleted` = 0
            join `products` on `products`.`id` = `basket_items`.`product_id`
            left join `products` `parentproduct` on `parentproduct`.`id` = `products`.`parent_id`
        group by `parentproduct`.`id`
        having count(*) >= 1
)
AND products.parent_id IS NULL AND products.deleted = 0
ORDER BY name ASC

I did come up with this however, it still takes into account product children not ordered for the same product parent – if that makes sense?

SELECT parent_products.id, parent_products.name, orders.id
FROM products
LEFT JOIN products parent_products ON products.parent_id = parent_products.id
LEFT JOIN basket_items ON basket_items.product_id = products.id AND basket_items.deleted = 0
LEFT JOIN baskets ON baskets.id = basket_items.basket_id
LEFT JOIN orders ON orders.basket_id = baskets.id
WHERE orders.id IS NULL
AND products.deleted = 0
AND parent_products.id IS NOT NULL
GROUP BY parent_products.name
ORDER BY parent_products.name ASC
  • 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-23T06:41:30+00:00Added an answer on May 23, 2026 at 6:41 am

    Good reason to rewrite this query. I would suggest moving the subquery into a view and then joining that view. Performance wise the select will be much faster.

    Alternatively,

    SELECT `products`.... 
    from `products`
        left join (
            select `parentproduct`.`id` 
                from `orders`
                    join `baskets` on `orders`.`basket_id` = `baskets`.`id`
                    join `basket_items` on `basket_items`.`basket_id` = `baskets`.`id` AND `basket_items`.`deleted` = 0
                    join `products` on `products`.`id` = `basket_items`.`product_id`
                    left join `products` `parentproduct` on `parentproduct`.`id` = `products`.`parent_id`
                group by `parentproduct`.`id`
                having count(*) >= 1
        ) as Counts
        ON products.id = Counts.id
        where Counts.id IS NULL AND products.parent_id IS NULL AND products.deleted = 0
        ORDER BY name ASC
    

    It still has a subquery but now the subquery is evaluated only once rather than per product id as you had above.

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

Sidebar

Related Questions

Hi all is it possible to rewrite query: select userid from User where userid
Is it possible to rewrite this extension method without the parameter? public static string
The original query looks like this (MySQL): SELECT * FROM books WHERE title LIKE
How can I rewrite the query select col1 from tbl1 so it splits the
I'm not even sure if this is possible. I am using Kohana framework(ver 2.3).
In JDBC I can use question marks for query parameters, like this: SELECT *
I have this contact list which I'm building using LINQ to SQL. The query
Is it possible to rewrite this loop: for k,n in [[aa,1],[ab,2], [ac,3], [ad,4], [ba,5],
I have four IF statements, is it possible to rewrite this into a neater
I am using Rails 2.3.5 and is wondering if it is possible to rewrite

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.