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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:57:14+00:00 2026-06-06T20:57:14+00:00

This has been bugging me for some time now, as a newbie I’m not

  • 0

This has been bugging me for some time now, as a newbie I’m not sure what I should exactly look for. After several hours of Googling I wasn’t able to find a satisfying answer.

I have a relatively complex query which I have stripped and simplified to become:

SELECT field_a1,
       field_a2,
       (SELECT Count(*)
        FROM   table_b
        WHERE  field_b1 IN (SELECT field_x1
                            FROM   table_x) AND field_b2 = 1) AS new_field_1,
       (SELECT Count(*)
        FROM   table_c
        WHERE  field_c1 IN (SELECT field_x1
                            FROM   table_x) AND field_c2 = 2) AS new_field_2,
       (SELECT Count(*)
        FROM   table_d
        WHERE  field_d1 IN (SELECT field_x1
                            FROM   table_x) AND field_d2 = 3) AS new_field_3
FROM   table_a
WHERE  field_a1 IN (SELECT field_x1
                    FROM   table_x)

In the original query, I have a about 30 of those new_field_*. Everything seems to work fine and I’m getting some very satisfying results (end results and performance).

My problem is that this query:

SELECT field_x1
FROM   table_x

is repeated a quite large number of times (30 * number of rows returned) and is always used as a set in which the WHERE IN will look for matches.

My question:

Is it possible to execute this small query only once and keep that result and use it repeatedly?

The only two (ridiculous) things I could think of were something like this (imaginary solution):

temp = (SELECT field_x1
        FROM   table_x)
SELECT field_1
FROM   table_1
WHERE  field_1 IN temp

Or executing this query once on the client side (a webserver running PHP) then appending the result to the future queries using something like this:

$IN_Condition = implode(',', $stmt->execute()->fetchAll(PDO::FETCH_NUM));
$sql_query = "SELECT field_1
              FROM   table_1
              WHERE  field_1 IN ($IN_Condition)"
  • 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-06T20:57:16+00:00Added an answer on June 6, 2026 at 8:57 pm

    Try to use JOIN clauses, e.g. –

    edited:

    SELECT
      field_a1,
      COUNT(b.field_b1) new_field_1,
      COUNT(c.field_c1) new_field_2,
      COUNT(d.field_d1) new_field_3
    FROM
      table_a a
    JOIN table_x x
      ON x.field_x1 = a.field_a1
    LEFT JOIN table_b b
      ON b.field_b1 = x.field_x1
    LEFT JOIN table_c c
      ON c.field_c1 = x.field_x1
    LEFT JOIN table_d d
      ON d.field_d1 = x.field_x1
    GROUP BY
      a.field_a1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been bugging me for some time now. My problem is the following.
This has been bugging me for quite some time now. All I want to
This has been bugging me for some time now, I will greatly appreciate the
So this has been bugging me for some time and I can't seem to
This question has been bugging me for some time. I always picture launching my
This question has been bugging me for some time. I've already developed a couple
This question has been bugging me for a long time now but essentially I'm
Have a question regarding something which has been bugging me for some time now.I'm
This is a question that's been bugging me for some time now: In photoshop/GIMP,
This is has been bugging me for quite some time. >> nil.id (irb):2: warning:

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.