[英]Mysql query using UNION
$sql = "SELECT `employee`.`employee_id`, `employee`.`employee_pre_code`, `employee`.`employee_code`, `employee`.`employee_name`, `employee`.`active`, `designation`.`designation_name`, `employeetype`.`employee_type_name`, `supervisor`.`supervisor_id`, `incharge`.`incharge_id`, `subsection`.`subsection_id`, `section`.`section_id`, `floor`.`floor_id`, `unit`.`unit_id`, `attendance_summery`.`present_days`,`attendance_summery`.`working_days`
FROM `employee` LEFT JOIN `designation` ON `employee`.`designation_id`=`designation`.`designation_id`
LEFT JOIN `employeetype` ON `employee`.`employee_type_id` = `employeetype`.`employee_type_id`
LEFT JOIN `supervisor` ON `supervisor`.`supervisor_id` = `employee`.`supervisor_id`
LEFT JOIN `incharge` ON `incharge`.`incharge_id` = `supervisor`.`incharge_id`
LEFT JOIN `subsection` ON `subsection`.`subsection_id` = `incharge`.`subsection_id`
LEFT JOIN `section` ON `section`.`section_id` = `subsection`.`section_id`
LEFT JOIN `floor` ON `floor`.`floor_id` = `section`.`floor_id`
LEFT JOIN `unit` ON `unit`.`unit_id` = `floor`.`floor_id`
WHERE `employee`.`unit_id` = '1' AND `employee`.`active` = 1
UNION
SELECT `employee`.`employee_id`, `employee`.`employee_pre_code`, `employee`.`employee_code`, `employee`.`employee_name`, `employee`.`active`, `designation`.`designation_name`, `employeetype`.`employee_type_name`, `supervisor`.`supervisor_id`, `incharge`.`incharge_id`, `subsection`.`subsection_id`, `section`.`section_id`, `floor`.`floor_id`, `unit`.`unit_id`, `attendance_summery`.`present_days`,`attendance_summery`.`working_days` FROM `employee`
LEFT JOIN `attendance_summery` ON `attendance_summery`.`employee_id` = `employee`.`employee_id`
WHERE `attendance_summery`.`payment_period_id` = 36
ORDER BY `employee`.`employee_name` ASC";
$query = $this->db->query($sql);
return $query->result_array();
A Database Error Occurred 发生数据库错误
Error Number: 1054
错误编号:1054
Unknown column 'attendance_summery.present_days' in 'field list'
“字段列表”中的未知列“ attendance_summery.present_days”
But the column 'attendance_summery.present_days'
exists. 但是存在
'attendance_summery.present_days'
列。
I failed to find the problem with the query. 我找不到查询问题。
Can any one help. 谁能帮忙。
attendance_summery table is not selected in the first query of the union. 在联合的第一个查询中未选择Attenance_summery表。
You will have to add ; 您将不得不添加;
LEFT JOIN `attendance_summery` ON `attendance_summery`.`employee_id` = `employee`.`employee_id
to the first select of your sql. 到您的sql的第一个选择。 You need to test each field and add them.
您需要测试每个字段并添加它们。
don't you think, attendance_summery
table should not be a part of FROM
clause of first query while it is part of select list
? 您难道不认为
attendance_summery
表是select list
一部分时,它不应该是第一个查询的FROM
子句的一部分吗? I cant see attendance_summery
table in FROM CALUSE
which is miss!! 我不能看到
attendance_summery
表在FROM CALUSE
这是小姐!
My main issue was to use multiple conditions in JOINing the tables. 我的主要问题是在联接表中使用多个条件。 At last added two conditions when joining
attendance_summery
table with AND keyword which joins the rows that only matches $paymentPeriodId
variable. 在加入时,最后增加了两个条件
attendance_summery
桌子和关键字它连接,只有匹配的行$paymentPeriodId
变量。 That fulfilled what I wanted. 满足了我想要的。 Again thanks for
Jerome Anthony
and Nitin Tripathi
. 再次感谢
Jerome Anthony
和Nitin Tripathi
。 My updated query is as: 我更新的查询为:
$sql = "SELECT `employee`.`employee_id`, `employee`.`employee_pre_code`, `employee`.`employee_code`, `employee`.`employee_name`, `employee`.`active`, `designation`.`designation_name`, `employeetype`.`employee_type_name`, `supervisor`.`supervisor_id`, `incharge`.`incharge_id`, `subsection`.`subsection_id`, `section`.`section_id`, `floor`.`floor_id`, `unit`.`unit_id`, `attendance_summery`.`present_days`, `attendance_summery`.`working_days` "
. "FROM `employee` "
. "LEFT JOIN `designation` ON `employee`.`designation_id`=`designation`.`designation_id`"
. "LEFT JOIN `employeetype` ON `employee`.`employee_type_id` = `employeetype`.`employee_type_id`"
. "LEFT JOIN `supervisor` ON `supervisor`.`supervisor_id` = `employee`.`supervisor_id`"
. "LEFT JOIN `incharge` ON `incharge`.`incharge_id` = `supervisor`.`incharge_id`"
. "LEFT JOIN `subsection` ON `subsection`.`subsection_id` = `incharge`.`subsection_id`"
. "LEFT JOIN `section` ON `section`.`section_id` = `subsection`.`section_id`"
. "LEFT JOIN `floor` ON `floor`.`floor_id` = `section`.`floor_id`"
. "LEFT JOIN `unit` ON `unit`.`unit_id` = `floor`.`floor_id`"
. "LEFT JOIN `attendance_summery` ON (`attendance_summery`.`employee_id` = `employee`.`employee_id` AND `attendance_summery`.`payment_period_id` = $paymentPeriodId )"
. "WHERE `employee`.`unit_id` = '1' AND `employee`.`active` = '1' "
. "ORDER BY `employee`.`employee_name` ASC";
$query = $this->db->query($sql);
return $query->result_array();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.