我有两张桌子。一个存储内容记录,另一个存储内容记录之间的关系。
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 |
+---+-----+------+------+ +---------+----------+
阅读表“关系”
id_local = 内容中记录的 ID(“父级”)
id_foreign = 与 id_local 相关的内容中的记录 ID(“子级”)
我希望 content.num = 222 的所有关系按照它们在表“关系”中输入的顺序。结果应如下所示:
result
+-----+------+
|num |value |
+-----+------+
|333 |34567 |
+-----+------+
|555 |56789 |
+-----+------+
|111 |12345 |
+-----+------+
我尝试了一些 JOIN,但从未得到这个结果。
请问,有人知道我怎样才能得到这个结果吗?
附加问题:
如果我想输出带有所有“子”记录的“父”记录的 content.value=23456,查询应该是什么样子?
result 2
+-----+------+--------+
|num |value | p-value|
+-----+------+--------+
|333 |34567 | 23456 |
+-----+------+--------+
|555 |56789 | 23456 |
+-----+------+--------+
|111 |12345 | 23456 |
+-----+------+--------+