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

  • Home
  • SEARCH
  • 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 8316071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:16:29+00:00 2026-06-08T21:16:29+00:00

I have query that work fine, but it has 2 the same subqueries SELECT

  • 0

I have query that work fine, but it has 2 the same subqueries

SELECT *, 
    (SELECT count(O.id) FROM `offer` O WHERE O.product_id = P.id) AS poffers 
FROM `product` P 
JOIN product_section PS ON (PS.product_id = P.id AND PS.section_id IN (14)) 
WHERE P.deleted is NULL 
    AND (SELECT count(O.id) FROM `offer` O WHERE O.product_id = P.id) > 0 

I try to optimize with JOIN, but new query returns empty result

SELECT *, OJ.pcount AS poffers
FROM `product` P 
JOIN product_section PS ON PS.product_id = P.id AND PS.section_id IN (14)
JOIN (SELECT count(O.id) AS pcount, O.product_id FROM `offer` O ) 
    AS OJ ON OJ.product_id = P.id
WHERE P.deleted is NULL AND OJ.pcount > 0 

Than i try variables, but i think i use it wrong, query returns empty result

SELECT *,  
    @o := (SELECT count(O.id) FROM `offer` O WHERE O.product_id = P.id) 
    AS poffers 
FROM `product` P 
JOIN product_section PS ON (PS.product_id = P.id AND PS.section_id IN (14)) 
WHERE P.deleted is NULL 
    AND @o > 0 
  • 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-08T21:16:31+00:00Added an answer on June 8, 2026 at 9:16 pm

    Avoid the dependent subqueries. Instead, have one query which relates products to offer counts. This query you can use in several places. It will most likely be stored temporarily in memory, avoiding duplicate computations, but the database engine might do more clever things as well.

    SELECT *, O.cnt AS poffers
    FROM product P
    JOIN product_section PS ON (PS.product_id = P.id AND PS.section_id IN (14))
    JOIN (SELECT product_id, count(id) AS cnt
          FROM offer
          GROUP BY product_id
         ) O ON (O.product_id = P.id)
    WHERE P.deleted is NULL AND O.cnt > 0
    

    If the relation between P and PS is one-to-one, then you could even work without any subquerys whatsoever:

    SELECT *, COUNT(O.id) AS poffers
    FROM product P
    JOIN product_section PS ON (PS.product_id = P.id AND PS.section_id IN (14))
    JOIN offer O ON (O.product_id = P.id)
    WHERE P.deleted is NULL
    GROUP BY P.id
    

    You also should consider whether there is a good reason to count a specific column. In most cases, COUNT(*) will be faster than COUNT(col). The only reason to use the latter is if you explicitely want to exclude NULL values from the count.

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

Sidebar

Related Questions

I have a query that looks like this SELECT * from myTable WHERE Date
I have a an query that I want to work on so I formatted
I have a problem with an sql query that will not work if I
i have some problems with a Query seem IN dosen't work with Group_concat, that
I have a query that successfully grabs the unique products from my products table
I have a query that basically combines tables of actions and selects from them
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,
I have a Linq-2-XML query that will not work if a google sitemap that
I have a page that grabs values from the query string using javascript window.location
I have a query that does some magical sort-group-joining that works just fine on

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.