繁体   English   中英

为什么我的SQL查询提取的行多于表中的行?

[英]Why does my SQL query fetching more rows than are in the table?

salary_sheet表中有35行,但是此查询获取30660行,不知道为什么?

 function queryCheck() {
  $sql = "select  salary_sheet.payment_period_id,salary_sheet.employee_id,salary_sheet.supervisorwise_serial,"
."salary_sheet.employee_type_id,salary_sheet.sl,salary_sheet.cl,salary_sheet.el,salary_sheet.basic,salary_sheet.hr,"
."salary_sheet.medical,salary_sheet.other,salary_sheet.total,salary_sheet.min_wages,"
."salary_sheet.prod_wages,salary_sheet.attn_bonus,salary_sheet.prod_bonus,"
."salary_sheet.ot_hours,salary_sheet.ot_rate,salary_sheet.ot_amount,salary_sheet.stamp_deduct,"
."salary_sheet.absent_deduction,salary_sheet.net_payable,"

."employee.employee_code,employee.employee_name,employee.designation_id,employee.employee_grade,"

."attendance_summery.working_days "
."from salary_sheet "
."left join employee on salary_sheet.employee_id = employee.employee_id  "
."left join attendance_summery on salary_sheet.payment_period_id = attendance_summery.payment_period_id    "
."where salary_sheet.employee_type_id = 9 and salary_sheet.payment_period_id = 41  "             
."order by salary_sheet.supervisorwise_serial desc";
$query = $this->db->query($sql);
$rows = $query->num_rows();
return $rows;

 }

有人的帮助表示赞赏。

这是您的查询。

select  salary_sheet.payment_period_id,
salary_sheet.employee_id,
salary_sheet.supervisorwise_serial,
salary_sheet.employee_type_id,salary_sheet.sl,salary_sheet.cl,salary_sheet.el,salary_sheet.basic,salary_sheet.hr,
salary_sheet.medical,salary_sheet.other,salary_sheet.total,salary_sheet.min_wages,
salary_sheet.prod_wages,salary_sheet.attn_bonus,salary_sheet.prod_bonus,
salary_sheet.ot_hours,salary_sheet.ot_rate,salary_sheet.ot_amount,salary_sheet.stamp_deduct,
salary_sheet.absent_deduction,salary_sheet.net_payable,
employee.employee_code,employee.employee_name,employee.designation_id,employee.employee_grade,
attendance_summery.working_days
from salary_sheet
left join employee 
on salary_sheet.employee_id = employee.employee_id
left join attendance_summery 
on salary_sheet.payment_period_id = attendance_summery.payment_period_id
where salary_sheet.employee_type_id = 9 and salary_sheet.payment_period_id = 41             
order by salary_sheet.supervisorwise_serial desc

第一个“左联接”在左边包含employee_type_id 9和payment_period_id 41的工作表行。 所有这些工作表都将保留。 在左侧联接的右侧 ,所有行都会出现,无论这些行是否存在于这些员工( 这就是左侧联接所做的事情 )。 因此,如果存在它们,它们就来;如果不存在,它们就为空值。

第一步的结果是在左联接上的表,对表Attenance_summary进行相同的操作。

因此,是的,当您无意中犯了一个错误时,左联接至少可以起到重复作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM