如何在要连接的 table 的名称是第一个 table 中的值的查询中连接两个 table

How to join two tables in a query where the name of the table to be joined is a value in the first table

我有两个查询将 4 个 table 连接在一起。第四个 table 是从第一个 table 读取的值。到目前为止,我一直将该值读入一个变量,然后在第二个查询中使用这些变量,但我想知道是否可以将其合并到一个查询中。

这是我的查询($node_type_name 是从调用函数传入的):

$this->read_db->select('id, head_node_id, data_table_name');
    $this->read_db->from('node_type');
    $this->read_db->where('name', $node_type_name);
    $Q = $this->read_db->get();
    $table_name = $Q->row_array()['data_table_name'];
    $field_name = $table_name . '.node_id';
    $this->read_db->select('node.id, node.name, node.is_head_node, node.node_type_id, node_link.parent_node_id, ' . $table_name . '.id, ' . $table_name . '.node_id');
    $this->read_db->from('node');
    $this->read_db->join('node_link', 'node_link.child_node_id = node.id');
    $this->read_db->join($table_name, $field_name . ' = node.id');
    $M = $this->read_db->get();

该函数是一个通用函数,因此有人可以将 locations 发送到 $node_type_nameorganizations,查询将加入与这些名称关联的 table .目前只有两种“类型”,但我们将来可能会添加更多,因此函数需要通用。

最后,我创建了一个单独的函数来获取对象中的第一个查询,然后将其传递给第二个函数。