Answer the question
In order to leave comments, you need to log in
How to make a cascading SQL query?
There are owners, they have directors, directors of the enterprise, and workers at the enterprises.
Obviously, according to the table in the database for each entity. In total, in order to find out which owner a particular worker is subordinate to, you need to:
SELECT `worker`,`id` FROM `workers` further in enterprises, request the id of the enterprise to which the id of this worker belongs, then one level higher request the id of the director whose enterprise, and then to the level above the id of the owner who is subordinate to the id of the selected director.
How it is better to construct such request, subqueries? There will be after all 3 or 4 nesting levels?
Answer the question
In order to leave comments, you need to log in
You say that like 3 or 4 levels of nesting is something bad.
Of course, you can rewrite everything to joins, but it's not a fact that it will be faster (you need to test it on a real structure and real data).
If such queries are frequently used in an application - this is a reason to denormalize tables, introduce pass-through identifiers into tables and reduce both fetch time and query complexity.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question