我试图加入这三个表: ![approved_loan] [2]
我设法轻松加入Employee
和Attendance
表,但无法使其适用于第三个表。这是我加入两个表的代码:
Approved_leave
表:
CREATE TABLE IF NOT EXISTS `approved_leave` (
`id` int(255) NOT NULL,
`company` varchar(70) NOT NULL,
`branch` varchar(70) NOT NULL,
`dept` varchar(70) NOT NULL,
`emp_id` varchar(255) NOT NULL,
`leave_type` varchar(70) NOT NULL,
`day_number` varchar(70) NOT NULL,
`reason` varchar(70) NOT NULL,
`applied_date` date NOT NULL,
`file` varchar(30) NOT NULL,
`approved_date` date NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
Attendance
表:
CREATE TABLE IF NOT EXISTS `attendance` (
`id` int(11) NOT NULL,
`company` varchar(20) NOT NULL,
`branch` varchar(55) NOT NULL,
`dept` varchar(100) NOT NULL,
`employee_id` varchar(255) NOT NULL,
`time` timestamp NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(12) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
Employee
表:
CREATE TABLE IF NOT EXISTS `employee` (
`emp_id` varchar(20) NOT NULL,
`emp_name` varchar(30) NOT NULL,
`emp_contact` varchar(30) NOT NULL,
`com_address` varchar(30) NOT NULL,
`per_address` varchar(30) NOT NULL,
`com_phone` varchar(13) NOT NULL,
`com_email` varchar(20) NOT NULL,
`empid` int(20) NOT NULL,
`emp_company` varchar(20) NOT NULL,
`emp_branch` varchar(20) NOT NULL,
`emp_dept` varchar(20) NOT NULL,
`emp_designation` varchar(20) NOT NULL,
`emp_salary` varchar(30) NOT NULL,
`emp_type` varchar(10 NOT NULL,
`last_updated` date NOT NULL,
`active` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
$this->db->select('COUNT(status),emp_name,employee_id,emp_salary')
->from('attendance a')->where('dept',$dept_id)
->join('employee e','a.employee_id=e.empid','left')->group_by('employee_id')
->get()->result();
批准的表格列如下。我的目标是获得员工的贷款金额,如果他有:
id, loan_amount, reason, employee_id, dept, branch, company applied_date
答案 0 :(得分:0)
尝试下面的mysql查询:
http://api.dev.example.com/fear_and_loathing_in_las_vegas