Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从MySql中选择all并加入其他_Php_Mysql_Codeigniter - Fatal编程技术网

Php 从MySql中选择all并加入其他

Php 从MySql中选择all并加入其他,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有这个codeigniter功能 function allClients($orderby = 'clients_company_name', $sort = 'ASC') { //profiling:: $this->debug_methods_trail[] = __function__; //declare $conditional_sql = ''; //check if any specifi ordering was passed

我有这个codeigniter功能

function allClients($orderby = 'clients_company_name', $sort = 'ASC')
{

    //profiling::
    $this->debug_methods_trail[] = __function__;

    //declare
    $conditional_sql = '';

    //check if any specifi ordering was passed
    if (! $this->db->field_exists($orderby, 'clients')) {
        $orderby = 'clients_company_name';
    }

    //check if sorting type was passed
    $sort = ($sort == 'asc' || $sort == 'desc') ? $sort : 'ASC';

    //----------sql & benchmarking start----------
    $this->benchmark->mark('code_start');

    //_____SQL QUERY_______
    $query = $this->db->query("SELECT *
                                      FROM clients
                                      ORDER BY $orderby $sort");

    $results = $query->result_array(); //multi row array

    //benchmark/debug
    $this->benchmark->mark('code_end');
    $execution_time = $this->benchmark->elapsed_time('code_start', 'code_end');

    //debugging data
    $this->__debugging(__line__, __function__, $execution_time, __class__, $results);
    //----------sql & benchmarking end----------

    //return results
    return $results;

}
它从表客户机中选择所有数据。其中之一是“客户\团队\个人资料\ id”-该客户的所有者

我还需要加入其他表-团队配置文件。在那里我们可以找到team_profile_id(系统中有用户id)和team_profile_full_name-用户名称

Table: clients

clients_id|clients-company_name|client_address|clients_team_profile_id
1         |Apple               |some street   |2
2         |Dell                |some street   |5


Table team_profile

team_profile_id | team_profile_full_name|
2               |John                   |
5               |Bob                    |
我需要从表CLIENTS中选择所有数据(如我们所见-select*),还需要获取连接到客户端的团队用户的名称,并为result设置一个参数-as f.ex。客户\所有者\名称


非常感谢您的帮助。

因此您可以将此连接添加到现有查询中

SELECT c.*, t.team_profile_full_name as client_owner_name
FROM clients c
    JOIN team_profile t ON t.team_profile_id = c.clients_team_profile_id
ORDER BY $orderby $sort"
您可能还需要更改这段代码才能像这样使用表别名

 //check if any specifi ordering was passed
if (! $this->db->field_exists($orderby, 'clients')) {
    $orderby = 'c.clients_company_name';
}
而且

function allClients($orderby = 'c.clients_company_name', $sort = 'ASC')

因此,您可以将此联接添加到现有查询中

SELECT c.*, t.team_profile_full_name as client_owner_name
FROM clients c
    JOIN team_profile t ON t.team_profile_id = c.clients_team_profile_id
ORDER BY $orderby $sort"
您可能还需要更改这段代码才能像这样使用表别名

 //check if any specifi ordering was passed
if (! $this->db->field_exists($orderby, 'clients')) {
    $orderby = 'c.clients_company_name';
}
而且

function allClients($orderby = 'c.clients_company_name', $sort = 'ASC')

如果进行联接不是一个很大的问题,而且您的查询是纯文本的,只需将联接添加到现有查询中即可。幸运的是,我不能这样做,您是否能够提供有用的帮助?除非您显示所有要查询数据的表的实际表定义。您的描述不清楚,这只会导致我的猜测,然后您说这不起作用,我们会反复讨论。当然,我编辑了我的问题。如果进行连接,这不是一个很大的问题,而且您的查询是纯文本的,只需将连接添加到您现有的查询中即可。幸运的是,我不能这样做,您能提供有用的帮助吗?除非您显示了所有要查询数据的表的实际表定义。你的描述不清楚,这只会导致我的猜测,然后你说那不起作用,我们一个接一个地说。当然,我编辑了我的问题