Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.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
我需要phpmysql复杂查询方面的帮助_Php_Mysql_Database_Codeigniter - Fatal编程技术网

我需要phpmysql复杂查询方面的帮助

我需要phpmysql复杂查询方面的帮助,php,mysql,database,codeigniter,Php,Mysql,Database,Codeigniter,我有三张桌子。表名为 工资历史记录表-员工id(外键) 员工表-员工id(主要)部门结构id(国外) 部门表-部门结构id(主要) 现在,salary_history_表有一个名为“employee_id”的id,它是employee_表中的主键。而且employee_表有一个名为“department_structured_id”的id,这是department_表中的主键,我的问题是我想根据department_表中的“department_structured_id”更新名为rate_Ex

我有三张桌子。表名为

  • 工资历史记录表-员工id(外键)
  • 员工表-员工id(主要)部门结构id(国外)
  • 部门表-部门结构id(主要)

  • 现在,salary_history_表有一个名为“employee_id”的id,它是employee_表中的主键。而且employee_表有一个名为“department_structured_id”的id,这是department_表中的主键,我的问题是我想根据department_表中的“department_structured_id”更新名为rate_Except的salary_history_表列中的所有数据。如何连接这三个表,并根据员工id更新与特定id查询相关的所有内容?

    这很简单,没有那么复杂。只需使用下面的查询,让我告诉您它是如何工作的,首先,您必须调用一个具有两个值的表,这里我在
    get()
    中调用它,一旦您获得了两个Id,您就可以使用这些Id连接另一个表

    $this->db->join('salary_history_table', 'salary_history_table.employee_id = employee_table.employee_id', 'left');
    $this->db->join('department_table', 'department_table.department_structure_id = employee_table.department_structure_id', 'left');
    $this->db->get('employee_table');
    

    如果您还有任何问题,请告诉我。

    首先,画出表格及其关系很有帮助(为了在手机上打字,我缩短了姓名)

    因此,如果您想列出所有部门所有员工的薪资历史记录

    select s.* 
    from department d 
    inner join employee e on e.department_id = d.department_id
    inner join salary_history s on s.employee.id = e.employee.id
    
    但是对于你的问题,如果你知道部门id,你甚至不需要加入所有三个表

    select s.*
    from salary_history s
    inner join employee e on e.employee_id = s.employee_id
    where e.department_id = ?
    
    但是如果你不知道部门id,你必须根据部门名称进行匹配

    select s.*
    from salary_history s
    inner join employee e on e.employee_id = s.employee_id
    inner join department d on d.department_id = e.department_id
    where d.name = ?
    
    使用此选项,您可以设置更新。语法在不同的db系统之间似乎有所不同,因此您必须了解哪些适合您

    update salary s
    inner join employee e on e.employee_id=s.employee_id
    inner join department d on d.department_id=e.department_id
    set rate_exempt = ?
    where d.name=?
    
    or,
    
    update ...
    set...
    inner join
    inner join
    where
    
    or,
    
    update salary
    set rate_exempt = ?
    where employee_id in (
        select e.employee_id
        from employee e
        inner join department d on d.department_id=e.department_id
        where d.name = ?
        )
    

    你试过什么?采样前后的数据是什么样的?编辑问题以包含这些详细信息。
    update salary s
    inner join employee e on e.employee_id=s.employee_id
    inner join department d on d.department_id=e.department_id
    set rate_exempt = ?
    where d.name=?
    
    or,
    
    update ...
    set...
    inner join
    inner join
    where
    
    or,
    
    update salary
    set rate_exempt = ?
    where employee_id in (
        select e.employee_id
        from employee e
        inner join department d on d.department_id=e.department_id
        where d.name = ?
        )