我在连接表时遇到问题,有一个关于连接两个表的条件。我有三个表让我们假设它是 table1、table2 和 table3,
table1
+---+
|id |
+---+
table2
+---------------+
|id | table1_id |
+---------------+
table3
+----------------------------+
| id | table1_id | table2_id |
+----------------------------+
现在,我的主表是“table3”,我需要将主表与 table1 & table2 以这样的方式连接,如果 table2_id 的值存在于 table3 中,那么 table2 应该与 table2_id & 同样如果 table1_id 退出然后 table1 将与table1_id连接,例如:table3的入口就是这样
+----------------------------+
| id | table1_id | table2_id |
| 1 | 1 | 0 |
| 2 | 0 | 1 |
+----------------------------+
for the value of id = 1,
table1_id exists & table2_id is zero, so table1 should be joined,
for the id = 2,
table2_id exists & table1_id is zero, so table2 should be joined,
if there is a case that both exists then table2 should be given the priority i.e, the table2 will be joined, can anyone make me out of this prb pls..