Search code examples
sqlentityself-joinentity-sql

In Entity SQL Is Self Join Possible?


I have a question in regards to entity SQL where I need to do a lookup within a table itself based on a particular column value. I don't really know how to word this question, but I'll do my best. Also I don't know the syntax of entity SQL very well and would love some help here. Basically I have 2 tables (table1 and table2). In table 1 I have names and folderID's. In table 2 I have folderID's parentFolderID's and FolderNames.

I need to create a select statement where I display each name from table1 along with it's parentFolder's name if that makes sense. I can easily get the parent folderID, but am confused as to how I can use that to JOIN(maybe?) the table with itself.

When trying to do this JOIN table2 on table2.folderID = table2.parentFolderID I receive this error, " Left correlation is not allowed in the JOIN clause". I'm not sure what this error means and again I am not familiar with ESQL and would love any help/tips. I've attached a photo of what I'm looking to accomplish in case my description was confusing.

Desired Output


Solution

  • if the parent child relation is only for one level you could use a self join on table2

    select  a.cust_name, c.folder_name 
    from table1 a 
    inner join table2 b on a.cust_folder_id =  b.folder_id 
    inner join table 2 c on b.parent_folder = c.folder_id