【发布时间】:2020-09-08 08:48:52
【问题描述】:
我有两个查询将 4 个表连接在一起。第四个表是从第一个表中读取的值。到目前为止,我一直在将该值读入一个变量,然后在第二个查询中使用这些变量,但我想知道是否可以将其合并到一个查询中。
这是我的查询($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_name 或organizations,查询将与与这些名称关联的表连接。目前只有两种“类型”,但我们将来可能会添加更多,因此函数需要是泛型的。
【问题讨论】:
-
对我来说,这听起来像是数据库设计中的一个巨大错误。
-
@CBroe,你能解释一下吗?
-
我猜你的表必须有非常相似的结构,因为你已经证明了什么是有意义的。但如果它们如此相似,那么这些数据很可能一开始就不应该分布在多个这样的表中。
标签: php mysql codeigniter