php - Get related rows in one table using a table with relations -


i have 2 tables. 1 stores content records, other 1 stores relations between content-records.

table "content"             table "relations"   +---+-----+------+------+   +---------+----------+   |id |num  |text  |value |   |id_local |id_foreign|   +---+-----+------+------+   +---------+----------+   |1  |111  |aaa   |12345 |   |2        |3         |   +---+-----+------+------+   +---------+----------+   |2  |222  |bbb   |23456 |   |2        |5         |   +---+-----+------+------+   +---------+----------+   |3  |333  |ccc   |34567 |   |4        |1         |   +---+-----+------+------+   +---------+----------+   |4  |444  |ddd   |45678 |   |2        |1         |   +---+-----+------+------+   +---------+----------+   |5  |555  |eee   |56789 |   |3        |6         |   +---+-----+------+------+   +---------+----------+   |6  |666  |fff   |67890 |   |4        |5         |   +---+-----+------+------+   +---------+----------+   

reading table "relations"

id_local = id of record in content ("parent")
id_foreign = id of record in content related id_local ("child")

i want relations content.num = 222 in order entered in table "relations". result should this:

result   +-----+------+   |num  |value |   +-----+------+   |333  |34567 |   +-----+------+   |555  |56789 |   +-----+------+   |111  |12345 |   +-----+------+   

i tried joins never got result.

please, know how can result?

additional question:

if want output content.value=23456 of "parent"-record "child"-records, how should query like?

result 2 +-----+------+--------+   |num  |value | p-value|   +-----+------+--------+ |333  |34567 | 23456  |   +-----+------+--------+   |555  |56789 | 23456  |     +-----+------+--------+   |111  |12345 | 23456  |   +-----+------+--------+   

i guess weird method but;

select num, value content id in (   select id_foreign   relations   id_local = ( select id content num = '222' )   )   , num = '222' -- additional answer 

Comments

Post a Comment

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -