【问题标题】:join 3 tables with codeigniter使用 codeigniter 连接 3 个表
【发布时间】:2017-01-20 10:01:17
【问题描述】:

在这里我想加入这三个表,即salarypayrollattendance。首先我想加入salary.salary_template_idemployee.salary_template_id,然后我想加入employee.user_idattendance.user_id为了得到每个user_id的基本工资。我该如何加入? 我的工资表是这样的

我收到了这个错误

“on 子句”中的未知列“tbl_employee_payroll.salary_template_id”

salary_template_id  salary_grade    basic_salary    overtime_salary
1                        a             10000    
2                        b             15000    

我的员工表是这样的

payroll_id  user_id     salary_template_id  
    1         1                NULL 
    2         36                2   
    3         43                1   

我的出席是这样的

 attendance_id  user_id     leave_category_id   date_in     date_out    attendance_status 


    13           36              0            2017-01-02    2017-01-02       1
    14           36              3            2017-01-04    2017-01-04       3

这是我的代码

public function attendance_report_by_empid($user_id = null, $sdate = null) {

    $this->db->select('attendance.*', FALSE);
    $this->db->select('employee.*', FALSE);
    $this->db->select('salary.*', FALSE);

    $this->db->from('attendance');
    $this->db->join('salary', 'salary.salary_template_id  = employee.salary_template_id', 'inner');
    $this->db->join('employee', 'employee.user_id  = attendance.user_id', 'left');
    $this->db->where('attendance.user_id', $user_id);
    $this->db->where('attendance.date_in', $sdate);
    $this->db->where('attendance.date_out <=', $sdate);

    $query_result = $this->db->get();
    $result = $query_result->result();

    return $query;
}

我的结果应该是这样的

attendance_id   user_id     leave_category_id   date_in     date_out    attendance_status  salary
    13           36              0            2017-01-02    2017-01-02       1              1000
    14           36              3            2017-01-04    2017-01-04       3              1000

【问题讨论】:

  • 您想告诉我们问题出在哪里吗?
  • 问题是无法加入并出现错误
  • 您想告诉我们您遇到的错误是什么吗?
  • Unknown column 'tbl_employee_payroll.salary_template_id' in 'on clause' 我收到此错误
  • 然后检查表和列的实际名称。错误信息很清楚,MYSQL不只是因为它变得无聊而弥补它们

标签: mysql codeigniter join


【解决方案1】:

根据您的愿望输出查询应该是这样的

$this->db->select('*'); 
$this->db->from('attendence');
$this->db->join('salary'.'employee.salary_template_id', 'attendence.salary_template_id = table2.id',);
$this->db->join('employee', 'employee.user_id = attendance.user_id');
$query = $this->db->get();

【讨论】:

  • 为什么 OP 应该“使用此代码”? 好的答案将始终解释所做的工作以及这样做的原因,不仅适用于 OP,而且适用于可能会发现此问题并正在阅读您的答案的 SO 的未来访问者。
【解决方案2】:

您在错误的表上应用联接

    $this->db->select('attendance.*', FALSE);
    $this->db->select('employee.*', FALSE);
    $this->db->select('salary.*', FALSE);
    $this->db->from('salary '); 
    $this->db->join('employee', 'salary.salary_template_id  = employee.salary_template_id', 'inner');
    $this->db->join('attendance', 'employee.user_id  = attendance.user_id', 'left');
    $this->db->where('attendance.user_id', $user_id);
    $this->db->where('attendance.date_in', $sdate);
    $this->db->where('attendance.date_out <=', $sdate);
    $query_result = $this->db->get();
    $result = $query_result->result();

【讨论】:

  • 你正在加入$this-&gt;db-&gt;from('attendance'); $this-&gt;db-&gt;join('salary', 'salary.salary_template_id = employee.salary_template_id', 'inner');出勤和工资,并使用员工加入
  • 抱歉重播晚了..我想以我想加入的方式从考勤表中获取它
【解决方案3】:

我通过像这样更改我的代码得到它

public function attendance_report_by_empid($user_id = null, $sdate = null) {

$this->db->select('attendance.*', FALSE);
$this->db->select('employee.*', FALSE);
$this->db->select('salary.*', FALSE);

$this->db->from('attendance');

$this->db->join('employee', 'employee.user_id  = attendance.user_id', 'left');
$this->db->join('salary', 'salary.salary_template_id  = employee.salary_template_id', 'inner');
$this->db->where('attendance.user_id', $user_id);
$this->db->where('attendance.date_in', $sdate);
$this->db->where('attendance.date_out <=', $sdate);

$query_result = $this->db->get();
$result = $query_result->result();

return $query;

}

问题是我们必须在使用该表进行连接之前声明该表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2015-03-24
    • 2022-10-07
    相关资源
    最近更新 更多