簡體   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