relationship - MySQL - Working Back Through Multiple Tables by FK -


i have database isn't of own creation. need extract specific data i'm struggling head around how data without doing multiple queries , looping on result set in code. i've looked around @ other questions haven't been able far.

my data structure (very condensed non-relevant rows , columns omitted):

mydb.source +--------+-------------+--------------------------------------+ | id     | source_type |  url                                 | +--------+-------------+--------------------------------------+ | 10     |          3  |  https://en.wikipedia.org            | +--------+-------------+--------------------------------------+  mydb.resource +--------+------------+--------------------------------------+ | id     | source_fk  |  identifier                          | +--------+------------+--------------------------------------+ | 1      |         10 |  all_saints_church,_marple           | +--------+------------+--------------------------------------+  mydb.item_base +--------+-------------+--------------------------------------+ | id     | resource_fk |  item_title                          | +--------+-------------+--------------------------------------+ | 55     |          1  |  saints church, marple           | +--------+-------------+--------------------------------------+  mydb.item +--------+-------------+--------------------------------------+ | id     | base_fk     |  item_description                    | +--------+-------------+--------------------------------------+ | 120    |         55  |  foo bar                             | +--------+-------------+--------------------------------------+ 

source - resource 1 many.

resource - base 1 1.

item_base - item 1 1.

what trying do?

i want few queries possible work mydb.source find items related it. information have in hand id source, 10. want end result set of item.id contains source.id 10.

i think can inner join 4 tables in single query. should safe, because in order relationship exist between source , item, latter must reachable via key relationship.

select     t1.id source_id,     t4.* source t1 inner join resource t2     on t1.id = t2.source_fk inner join item_base t3     on t2.id = t3.resource_fk inner join item t4     on t3.id = t4.base_fk     t1.id = 10 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -