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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100