D
D
DirDir2015-11-18 23:40:25
MySQL
DirDir, 2015-11-18 23:40:25

How to query data from MySQL table through multilevel relationship?

Structure
There are several tables, let's call them T1, T2, T3, T4.
From each of these tables with a lower index there is a link to a table with a higher one. One-to-many relationship.
For example, in table T2 there are fields `id`, `t1_id`, `name`- where `t1_id`is the identifier of the related record from the parent table. Similarly, the structure for T3 is - `id`, `t2_id`, `name`, and for T4 - `id`, `t3_id`, `name`.
Essence of the question
A sample is made from T4, in this sample it is necessary to add the value `name`from T1 obtained by "steps up". Data from T2 and T3 is not needed in the sample. This operation can be performed via JOIN or subqueries, but I would like to make it shorter, just relying on the relational structure (which is drawn graphically in phpmyadmin), something like:

SELECT `T4`.`id`, `T4`.`name`, `T1`.`name` AS `name_from_t1` FROM `T4`

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question