Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Join and derive relation when more than single column has foreign key reference to another table

I have table role_relations where columns are referencing same another table called ‘roles’. Currently i’m joining twice. Is there better way where join once and derive ‘name’ ?

SELECT r1.name, r2.name FROM role_relations rr
JOIN roles r1 ON rr.id = r1.id
JOIN roles r2 ON rr.parent_id = r2.id
WHERE rr.role_relation_id = :rrId;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It is normal if you have hierarchy data to need to join the definitions twice. I doubt you have performance issues, as it’s common for security hierarchy (roles, groups) to perform such queries and basically the data is not huge (you have certain number of roles, not billions of them).

If you are concern about the double read of the data you can read the role_relations and roles once and build the output in the application.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading