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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:28:32+00:00 2026-05-25T17:28:32+00:00

Following problem: Query 2 and Query 3 depend on the results of Query 1

  • 0

Following problem:

Query 2 and Query 3 depend on the results of Query 1 and Query 4 depends on the results of query 2. How would you express these queries without executing the same query multiple times?

Example:

Query 1

SELECT id, color, part FROM T1

Query 2

SELECT id, owner FROM T2 WHERE T2.color in (SELECT id, color, part FROM T1)

Query 3

SELECT id from T3 where T3.part in (SELECT id, color, part FROM T1)

Query 4

SELECT id from T4 where T4.owner in (SELECT id, owner FROM T2 WHERE T2.color in (SELECT id, color, part FROM T1))

edit

At the end i need the union of the result

Query1 union Query2 union Query3 union Query4

Now as you can see, I have copied and pasted the previous queries, there must be a better way of doing this.

  • 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-25T17:28:33+00:00Added an answer on May 25, 2026 at 5:28 pm

    Do you really need four different result sets? How many rows are you expecting in each set and what kind of entities are T1, T2, T3, T4 – because it may be possible to combine those queries into a single set.

    In addition, this:

    SELECT id, owner FROM T2 WHERE T2.color in (SELECT id, color, part FROM T1)
    

    isn’t valid SQL, you probably mean:

    SELECT id, owner FROM T2 WHERE T2.color in (SELECT color FROM T1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem. I wrote a query to get the date of
I'm looking for help to create a query, to solve the following problem: Let's
I am using following query which works fine for me except one problem SELECT
I face the following problem : I encrypt my query string then encoding the
I have the following problem: on a very simple php-mysqli query: if ( $result
I have the following problem that needs to be solved in a MySQL query:
Problem: Query aborts after 10 minutes Query should be faster I created the following
I have the following problem with an NHibernate query. one table LearnerDocuments : LearnerNumber
Following problem: Table with 1000 000 Records If i use the following query with
I need a Mysql query for my following problem. I have two tables. Exam

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.