gpt4 book ai didi

php - 我正在尝试使用来自 codeigniter 的事件记录类从多个表中查询

转载 作者:行者123 更新时间:2023-11-30 22:19:14 24 4
gpt4 key购买 nike

我正在尝试获取来自 mysql 查询的数据,以便稍后使用。我不在乎是否必须将数据输出到带有连接子句的表中。我只需要能够从中得到一个特定的部分。但是我想得到所有相关的或彼此相关的。

这是用 php 的 codeigniter MVC 框架制作的

example database with tables

我希望能够通过一次查询访问每个表中的数据

function get_reg(){
$this->db->select('*');
$this->db->from('
tableA.*,
tableB.*,
tableC.*,
tableD.*
');
$this->db->where('tableA.name = tableB.name');
$this->db->where('tableC.name = tableD.name');
$this->db->where('tableA.name = tableD.name');
$this->db->where('tableC.name = tableB.name');
$query = $this->db->get();
return $query->result_array();
}

像这样的东西然后像这样访问:

$this->load_model->get_reg()//得到我想要的

我不知道这是否可行。

最佳答案

要在单个查询中执行此操作,您需要使用 JOIN 语法。

你的答案看起来像

$this->db->from('tableA');
$this->db->join('tableB', 'tableA.name = tableB.name', 'LEFT'); // the type of join depends on the behavior you want
$this->db->join('tableC', 'tableA.name = tableC.name', 'LEFT');
$this->db->join('tableD', 'tableA.name = tableD.name', 'LEFT');
$query = $this->db->get();

以上根据名称将所有表连接在一起,该名称在所有表中似乎都相同。当连接表的某些行没有名称值时,连接类型很重要。

您可以在 docs 中阅读更多内容

关于php - 我正在尝试使用来自 codeigniter 的事件记录类从多个表中查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37145187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com