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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:34:28+00:00 2026-06-17T06:34:28+00:00

Consider the following (1:N) relationship: [entity: user] <—— rid key ——> [entity: rid] .

  • 0

Consider the following (1:N) relationship:

[entity: user] <------ rid key ------> [entity: rid].

consider the data in both tables as:

select * from user;
user-id        rid-key
a-basa         a
b-basa         b
a.a-basa       a.a   
a.b-basa       a.b
a.a.a-basa     a.a.a
a.a.b-basa     a.a.b
a.b.a-basa     a.b.a
a.b.b-basa     a.b.b
a.b.b.a-basa   a.b.b.a
a.b.b.b-basa   a.b.b.b



select * from rid;

rid-key    parent-rid    enabled
a            null        true
b            null        true
a.a          a           true 
a.b          a           false
a.a.a        a.a         true
a.b.a        a.b         true
a.b.b        a.b         true
a.b.b.a      a.b.b       true
......
n rows

I need to design a single query (not stored procedure) which will input a user-id, and the following facts are considered:

If an user is given access to a rid, then it can also access the parent rid of the rid given – the rid itself is enabled (enabled = true).

This should continue till we reach the root rid, ie. parent rid property is null.

In above example, the list of accessible rid for the user 'a.b.b.a-basa' will be:

a.b.b.a
a.b.b
a.b

and for a.a.a-basa:

a.a.a
a.a
a

can we get this list using a single query? Any sql vendor is fine.

  • 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-17T06:34:29+00:00Added an answer on June 17, 2026 at 6:34 am

    There are several models for Hierarchical data. Most models (like the Adjacency List) require some sort of recursion for some queries. With your design that uses the Materialized Path model, what you want is possible without a recursive query.

    Tested in MySQL (that has no recursive queries), at SQL-fiddle test-mysql. It can be easily converted for other DBMS, if you modify the string concatenation part:

    SELECT 
         COUNT(*)-1 AS steps_up,
         rid2.rid_key AS ancestor_rid_key
    FROM 
        u2 
      JOIN
        rid 
          ON u2.rid_key = rid.rid_key
          OR u2.rid_key LIKE CONCAT(rid.rid_key, '.%')
      JOIN
        rid AS rid2 
          ON rid.rid_key = rid2.rid_key
          OR rid.rid_key LIKE CONCAT(rid2.rid_key, '.%')
    WHERE
        u2.userid = 'basa'
      AND
        u2.rid_key = 'a.b.b.a' 
    GROUP BY 
        rid2.rid_key, rid2.enabled 
    HAVING 
        COUNT(*) + (rid2.enabled = 'true') 
      = SUM(rid.enabled = 'true') + 1 ;
    

    It uses this view, which is not strictly needed but it shows that the user.user_id is storing data that you already have in the rid_key column.

    CREATE VIEW u2 AS
    SELECT 
        SUBSTRING_INDEX(user_id, '-', -1) AS userid
      , rid_key
    FROM user ;
    

    One more note is that the above query does not use the parent_rid column at all. And that I’m sure it can be further improved.

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

Sidebar

Related Questions

Consider that i have two entities with following relationship: Entity A <-->> Entity B
Consider the following tables: Base, Primary key: Id Extension, Primary key: Id The primary
Consider the following parent/child relationship where Parent is 1..n with Kids (only the relevant
Consider the following Parent/Child relationship. What I would like to know is how I
I have a parent child relationship stored in a table consider the following example
Consider the tables: ( -> denotes a SQL defined relationship) USER (userid, username, imageid->IMAGE.imageid)
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following declarative User model in SQLAlchemy: class User(Base): id = Column(Integer, primary_key=True)
Consider the following entity model: public class Agreement : Entity { public int AgreementId
Consider the following tables (unnecessary fields have been omitted) and the relationships I'm trying

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.