douxuan0698 2015-12-13 18:13
浏览 48
已采纳

从MySql中选择全部并加入其他

I have this codeigniter function

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;

}

It selects all data from table clients. One of them is "client_team_profile_id" - the owner of this client.

I also need to join other table - team profiles. There we can find team_profile_id (there are ids of users in system) and team_profile_full_name - names of users.

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                    |

I need to select all data from table CLIENTS (as we can see - Select *) and also get name of team user connected to client and set a parameter for result - AS f.ex. client_owner_name.

I appreciate your help.

  • 写回答

1条回答 默认 最新

  • dongmo20030416 2015-12-13 19:36
    关注

    So you can add this JOIN to your existing query

    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"
    

    You may also need to change this bit of code to use the table alias like so

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

    And also

    function allClients($orderby = 'c.clients_company_name', $sort = 'ASC')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值