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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:48:47+00:00 2026-05-13T20:48:47+00:00

I have below a working query that needs to be simplified. The reason is

  • 0

I have below a working query that needs to be simplified.

The reason is that I need to expand it a lot to cover the real application. For each condition (Pos=xxx AND Indata=yyy) the current query doubles in size, and I have a lot of conditions. Also the ON clause will contain many more conditions than in the example….

The real application will have about 20 Pos/Indata conditions (only 3 here) and 20 other fields that must match (only 1 here).

What the query does, explained in “pseudocode”:

find all rows with (Pos=xx1 AND Indata=yy1) as t1
union
all rows with (Pos=xx2 AND Indata=yy2) as t2
then join t1 and t2 to keep only rows where other fields match (t1.fields=t2.fields OR t1.fields=* OR t2.fields=*)
you could store this in a temp table temp1

then find all rows with (Pos=xx3 AND Indata=yy3) as t3
union
temp1
then join t3 and temp1 to keep only rows where other fields match (t3.fields=temp1.fields OR t3.fields=* OR temp1.fields=*)
you could store this in a temp table temp2

then find all rows with (Pos=xx4 AND Indata=yy4) as t4
union
temp2
join t4 and temp4 to keep only rows where other fields match (t4.fields=temp4.fields OR t4.fields=* OR temp4.fields=*)
you could store this in a temp table temp2

etc, etc….I basically want to find all rows table “codes” with certain Pos and Indata where most other fields match each other….a possible solution may be temp tables plus a bit of PHP as in the pseudocode above….but it would be neat to solve in SQL only….

SELECT t15.* FROM (
    (
        SELECT DISTINCT t6.* FROM (
            (
                SELECT t5.* FROM (
                    (
                        SELECT DISTINCT t1.* FROM (
                            (SELECT * FROM codes WHERE (Pos = 10 AND Indata = 'Rexroth')) AS t1
                            JOIN (
                                (SELECT * FROM codes WHERE (Pos = 30 AND Indata = '%Mineralolja')) AS t2
                            ) ON (t1.Manufacturer = t2.Manufacturer OR t1.Manufacturer='*' OR t2.Manufacturer='*')
                        )
                    ) UNION (
                        SELECT DISTINCT t4.* FROM (
                            (SELECT * FROM codes WHERE (Pos = 10 AND Indata = 'Rexroth')) AS t3
                            JOIN (
                                (SELECT * FROM codes WHERE (Pos = 30 AND Indata = '%Mineralolja')) AS t4
                            ) ON (t3.Manufacturer = t4.Manufacturer OR t3.Manufacturer='*' OR t4.Manufacturer='*')
                        )
                    )
                ) AS t5
            ) AS t6

            JOIN (
                (
                    SELECT * FROM codes WHERE (Pos = 70 AND Indata = '28 cm3')
                ) AS t7
            ) ON (t7.Manufacturer = t6.Manufacturer OR t7.Manufacturer='*' OR t6.Manufacturer='*')
        )
    ) UNION (
        SELECT DISTINCT t14.* FROM (
            (
                SELECT t12.* FROM (
                    (
                        SELECT DISTINCT t8.* FROM (
                            (SELECT * FROM codes WHERE (Pos = 10 AND Indata = 'Rexroth')) AS t8
                            JOIN (
                                (SELECT * FROM codes WHERE (Pos = 30 AND Indata = '%Mineralolja')) AS t9
                            ) ON (t9.Manufacturer = t8.Manufacturer OR t9.Manufacturer='*' OR t8.Manufacturer='*')
                        )
                    ) UNION (
                        SELECT DISTINCT t11.* FROM (
                            (SELECT * FROM codes WHERE (Pos = 10 AND Indata = 'Rexroth')) AS t10
                            JOIN (
                                (SELECT * FROM codes WHERE (Pos = 30 AND Indata = '%Mineralolja')) AS t11
                            ) ON (t11.Manufacturer = t10.Manufacturer OR t11.Manufacturer='*' OR t10.Manufacturer='*')
                        )
                    )
                ) AS t12
            ) AS t13
            JOIN (
                (
                    SELECT * FROM codes WHERE (Pos = 70 AND Indata = '28 cm3')
                ) AS t14
            ) ON (t13.Manufacturer = t14.Manufacturer OR t13.Manufacturer='*' OR t14.Manufacturer='*')
        )
    )
) AS t15

I just rewrote it as follows with temp tables, seems easier to read at least:

CREATE TEMPORARY TABLE temp
SELECT * FROM codes WHERE (Pos = 10 AND Indata = 'Rexroth');

CREATE TEMPORARY TABLE temp2
SELECT DISTINCT t1.* FROM (
    (SELECT * FROM temp) AS t1
    JOIN (
        (SELECT * FROM codes WHERE (Pos = 30 AND Indata = '%Mineralolja')) AS t2
    ) ON (t1.Manufacturer = t2.Manufacturer OR t1.Manufacturer='*' OR t2.Manufacturer='*')
);
INSERT INTO temp2
SELECT DISTINCT t2.* FROM (
    (SELECT * FROM temp) AS t1
    JOIN (
        (SELECT * FROM codes WHERE (Pos = 30 AND Indata = '%Mineralolja')) AS t2
    ) ON (t1.Manufacturer = t2.Manufacturer OR t1.Manufacturer='*' OR t2.Manufacturer='*')
);

DROP TABLE temp;
ALTER TABLE temp2 RENAME temp;

CREATE TEMPORARY TABLE temp2
SELECT DISTINCT t1.* FROM (
    (SELECT * FROM temp) AS t1
    JOIN (
        (SELECT * FROM codes WHERE (Pos = 70 AND Indata = '28 cm3')) AS t2
    ) ON (t1.Manufacturer = t2.Manufacturer OR t1.Manufacturer='*' OR t2.Manufacturer='*')
);
INSERT INTO temp2
SELECT DISTINCT t2.* FROM (
    (SELECT * FROM temp) AS t1
    JOIN (
        (SELECT * FROM codes WHERE (Pos = 70 AND Indata = '28 cm3')) AS t2
    ) ON (t1.Manufacturer = t2.Manufacturer OR t1.Manufacturer='*' OR t2.Manufacturer='*')
);

SELECT * FROM temp2;
  • 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-13T20:48:47+00:00Added an answer on May 13, 2026 at 8:48 pm

    There’s a lot to be said for a stored proc and temp tables here – as you yourself suggest.

    For example your SQL above is getting quite hard to read even now: I look at it and immediately think “too long, really don’t want to read” 🙂

    Procs and working tables may make life much easier for future developers to read and extend – yourself included in 6 months time!

    You may also get performance benefits from procs and working tables: you can put indexes on the working tables, debug your proc one bit at a time to see where bottlenecks are, etc.

    I guess I’m recommending: favour readability and extensibility over “elegance” of using a single query, if performance can be considered more or less equivalent in either approach.

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

Sidebar

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.