我正在尝试从codeigniter的多个表中查询usign活动记录类 [英] I'm trying to query from multiple tables usign active record class from codeigniter

查看:44
本文介绍了我正在尝试从codeigniter的多个表中查询usign活动记录类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm tryig to get the data that comes from the mysql query to use it later. I don't care if I have to output the data in a table with join clauses. I only need to be able to get a speficic part from it. But i want to get all the ones that are related or have relevance to each other.

这是通过带有PHP的codeigniter MVC框架制成的

this is made with the codeigniter MVC framework with php

带有表的示例数据库

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

I want to be able to access the data from each table with one query

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()//并得到我想要的

$this->load_model->get_reg() //and get what I want

我不知道这是否可行。

I dont know if this is posible.

推荐答案

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

To do that in a single query, you need to use JOIN syntax.

您的答案将类似于

$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();

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

The above joins all the tables together based on the name, which appears to be the same in all the tables. The type of join matters when some rows of the joined tables don't have a value for name.

您可以在文档

这篇关于我正在尝试从codeigniter的多个表中查询usign活动记录类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆