// initialized null variables
$sql_for_training = null;
$sql_for_screening = null;
$sql_for_all = null;
$sqlforpositions = null;
$sql0 = 'SELECT * FROM lib_positions';
$result0 = $db->fetchAll($sql0);
$city_query = "";
// append additional query
if(isset($data['region_id'])){
$sql_for_training.=" AND p.region = '".$data['region_id']."' ";
$sql_for_screening.=" AND p.region_address = ".$data['region_id']." ";
$sql_for_all.= " AND ".$data['table'].".region_id = ".$data['region_id'];
// $sqlforpositions = "";
}
if(isset($data['province_id'])){
$sql_for_training.=" AND p.province = ".$data['province_id']." ";
$sql_for_screening.=" AND p.province_address = ".$data['province_id']." ";
$sql_for_all.= " AND ".$data['table'].".province_id = ".$data['province_id'];
// $sqlforpositions = "";
}
if(isset($data['city_id'])){
$sql_for_training.=" AND p.city_municipality = ".$data['city_id']." ";
$sql_for_screening.=" AND p.city_municipality_address = ".$data['city_id']." ";
$sql_for_all.= " AND ".$data['table'].".city_municipality_id = ".$data['city_id'];
$sqlforpositions = "";
$city_query = "LIMIT 1";
}
// build query string.
$sql = "SELECT ".$data['table'].".id AS table_id, ".$data['table'].".".$data['type']."_desc AS ".$data['type']."_desc,
l_p.position_desc as position_desc,
(SELECT count(p.id) FROM tbl_personal_info AS p
INNER JOIN tbl_screening AS s ON p.id = s.candidate_id
INNER JOIN tbl_position_applied AS p_a ON s.candidate_id = p_a.candidate_id
WHERE p.".$data['type']."_address = ".$data['table'].".id
AND p_a.position_id = l_p.id
".$sql_for_screening.") AS screening,
(SELECT count(p.id) FROM tbl_personal_info AS p
INNER JOIN tbl_screening AS s ON p.id = s.candidate_id
INNER JOIN tbl_position_applied AS p_a ON s.candidate_id = p_a.candidate_id
WHERE s.screening_id = 6 AND
p.".$data['type']." = ".$data['table'].".id
AND p_a.position_id = l_p.id
".$sql_for_screening.") AS prescreened,
(SELECT count(p.id) FROM tbl_personal_info AS p
INNER JOIN tbl_screening AS s ON p.id = s.candidate_id
INNER JOIN tbl_position_applied AS p_a ON s.candidate_id = p_a.candidate_id
WHERE s.screening_id = 4 AND
p.".$data['type']." = ".$data['table'].".id
AND p_a.position_id = l_p.id
".$sql_for_screening.") AS qualified,
(SELECT count(p.id) FROM tbl_personal_info AS p
INNER JOIN tbl_training AS tr ON p.id = tr.candidate_id
INNER JOIN tbl_position_applied AS p_a ON tr.candidate_id = p_a.candidate_id
WHERE p.".$data['type']." = ".$data['table'].".id
AND p_a.position_id = l_p.id
".$sql_for_training.") AS training,
(SELECT count(p.id) FROM tbl_personal_info AS p
INNER JOIN tbl_training AS tr ON p.id = tr.candidate_id
INNER JOIN tbl_position_applied AS p_a ON tr.candidate_id = p_a.candidate_id
WHERE tr.status = 2 AND p.".$data['type']." = ".$data['table'].".id
AND p_a.position_id = l_p.id
".$sql_for_training.") AS failed,
(SELECT count(p.id) FROM tbl_personal_info AS p
INNER JOIN tbl_training AS tr ON p.id = tr.candidate_id
INNER JOIN tbl_position_applied AS p_a ON tr.candidate_id = p_a.candidate_id
WHERE tr.status = 1 AND p.".$data['type']." = ".$data['table'].".id
AND p_a.position_id = l_p.id
".$sql_for_training.") AS passed,
(SELECT count(p.id) FROM tbl_personal_info AS p
INNER JOIN tbl_requirements AS t_r ON p.id = t_r.candidate_id
INNER JOIN tbl_position_applied AS p_a ON p.id = p_a.candidate_id
WHERE (SELECT count(t_r.candidate_id) FROM tbl_requirements AS t_r WHERE candidate_id = p.id) = (SELECT count(l_r.id) FROM lib_requirements as l_r)
AND p_a.position_id = l_p.id
".$sql_for_training.") AS completed,
t.target as target,
t.screening_target as screening_target,
t.training_target as training_target
FROM ".$data['table']."
RIGHT JOIN tbl_target as t on ".$data['table'].".id = t.area_id
LEFT JOIN lib_positions as l_p on t.position_id = l_p.id
WHERE t.area_type ='".$data['type']."'"
.$sql_for_all;
// execute query string
// echo "<pre>".$sql."</pre>";
$result = $db->fetchAll($sql);
$db->closeConnection();
//Zend_Debug::dump($result);
//die();
return $result;
当我提取10行时。这是我想要的结果。但是当查询试图获取超过15行的查询时。我导致内部服务器500错误。我目前在tbl_personal_info中有2300行。我的tbl_region中的7个区域和我的lib_provinces表中的大约200多个省以及1000+ lib_city_municipality。我的查询错了吗?或者我只是使用了太多的子查询?谢谢你的帮助。
答案 0 :(得分:0)
问题已解决
选择 lr.id AS table_id, lr。&#34;。$ data [&#39; type&#39;]。&#34; _desc AS&#34;。$ data [&#39; type&#39;]。&#34; _desc, lp.position_desc, count(lr.id)AS count_area, count(pi.id)AS筛选, count(case ts.screening_id当6然后1 else null结束)AS预先筛选, count(case ts.screening_id当4然后1 else null结束)AS合格, count(tt.id)AS训练, count(case tt.status when 2 then 1 else null end)AS失败, count(case tt.status when 1 then 1 else null end)AS通过, 0 AS完成, tar.target作为目标, tar.screening_target as screening_target, tar.training_target as training_target
FROM tbl_personal_info AS pi
RIGHT JOIN tbl_position_applied AS tpa ON pi.id = tpa.candidate_id
RIGHT JOIN ".$data['table']." AS lr ON pi.".$data['type']." = lr.id
LEFT JOIN lib_positions AS lp ON tpa.position_id = lp.id
INNER JOIN tbl_screening AS ts ON pi.id = ts.candidate_id
LEFT JOIN tbl_training AS tt ON pi.id = tt.candidate_id
LEFT JOIN tbl_target AS tar ON lr.id = tar.area_id
WHERE tar.area_type = '".$data['type']."'
AND tar.position_id = lp.id
".$sql_for_all."
GROUP BY lr.id,tpa.position_id
ORDER BY lr.id ASC,lp.id ASC
我使用了这个改进的查询。 :))