O
O
Optimus2017-07-13 00:47:53
MySQL
Optimus, 2017-07-13 00:47:53

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

3 answer(s)
S
Stalker_RED, 2017-07-13
@Stalker_RED

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).

T
ThunderCat, 2017-07-13
@ThunderCat

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.

D
d-stream, 2017-07-13
@d-stream

What is the problem? Hundreds of thousands of lines - this is for the database - not the volume.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question