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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:13:37+00:00 2026-05-28T00:13:37+00:00

Hope the title is not that confusing – honestly said I had no idea

  • 0

Hope the title is not that confusing – honestly said I had no idea how to better explain my problem in a single line 🙂 (and Google it properly BTW)

A table ‘group’ is structured as a nested set to create tree menues. Articles in a table ‘article’ reference to ID of such group element to – well – group them:

Table 'article'                     Table 'group'
id   |   article      |   groupid           id   |  title    |   left    |   right
1    |   Bowl         |   2                 1    |  material |   1       |   6
2    |   Bowl         |   5                 2    |  wood     |   2       |   3
3    |   Cube         |   3                 3    |  steel    |   4       |   5
4    |   Cube         |   6                 4    |  shape    |   6       |   13
5    |   Bowl         |  10                 5    |  circle   |   7       |   8
6    |   Pyramid      |   2                 6    |  square   |   9       |   10
7    |   Pyramid      |   3                 7    |  rectangle|   11      |   12
8    |   Pyramid      |  11                 8    |  color    |   14      |   21
9    |   Bowl         |  11                 9    |  red      |   15      |   16
10   |   Cube         |   9                10    |  green    |   17      |   18
11   |   Pyramid      |   9                11    |  blue     |   19      |   20

To select any combination of group elements, I simply have a query mentioning a range from group.left to group.right, giving me a set of group.id that I can compare with article.groupid.
Following statement responds all articles consisting of wood:

SELECT
   a.article
FROM
   article AS a
LEFT JOIN 
   group AS g
ON (
      (
         2 >= g.left
      AND
         3 <= g.right
      AND
         g.id = a.groupid
      )
   )
GROUP BY
   a.article

Now my question: how should I create a query, that responds with e.g. all articles that consist of wood AND have ANY color?
I expected that repeating concatenated subqueries in the WHERE clause would make the deal:

SELECT
    a.article
FROM
    article AS a
WHERE
    a.groupid IN
        (
            SELECT
                CONCAT(g.id) AS gr
            FROM
                group AS g
            WHERE
                (
                    2 >= g.left 
                AND
                    3 <= g.right
                )
        )
AND
    a.groupid IN
        (
            SELECT
                CONCAT(g.id) AS gr
            FROM
                group AS g
            WHERE
                (
                    14 >= g.left 
                AND
                    21 <= g.right
                )
        )

This query responds no result. BTW I dont like subqueries but many efforts with JOINS and Sub-JOINS did not work as well.
My mind is spinning – any tips from you guys?

Thanks

  • 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-28T00:13:38+00:00Added an answer on May 28, 2026 at 12:13 am

    edited: now working. Test it here (but in MSSQL)

    First approach based on your query, problem comes from non well normalized design. To do the query you will to check if exists a row on articles with same name that references a kind of property:

    SELECT
        distinct a.article
    FROM
        article AS a
    WHERE
        exists 
          (      
               SELECT
                    1
                FROM
                    group AS g
                INNER JOIN
                    article as a2
                    on g.id = a2.groupid
                WHERE 
                    a2.article = a.article and
                    (2 <= g.left AND 3 >= g.right)
           ) and
          exists (
               SELECT
                    1
                FROM
                    group AS g
                INNER JOIN
                    article as a2
                    on g.id = a2.groupid
                WHERE 
                    a2.article = a.article and
                    (14 <= g.left AND 21 >= g.right)
            )
    

    Edited

    OP uses nested sets. But perhaps article’s table is still not well normalized. I suggest to take in consideration this design:

    article( id, name )
    groupNestedSet( id, title, left, right )
    article_group( idArticle, idGroup)
    

    sample normalized data:

    article( id, name )
    1, Bowl   (only one time)
    3, Cube   (also only one time)
    
    groupNestedSet( id, title, left, right )
    -- your data --
    
    article_group( idArticle, idGroup)
    1, 2
    1, 5
    3, 3
    3, 5
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope the title isn't too confusing, I'll try to explain better below. Suppose
I hope the title is not too confusing. I am trying to make folders
Not sure if that was the best Title in the world but I hope
I hope the title did not mislead you. My problem is the following: Currently
I'm not sure how to explain this problem so the title is kind of
The title is probably not accurate but I hope that reading this post you
I hope the title explains what I'd like to achieve. If not, please read
I hope the title and following text are clear, I'm not very familiar with
I hope the title makes sense. I have a set of items that I
I hope the title was good enough to help explain what I am having

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.