2011-05-05 162 views
3

我試圖創建一個變化的基礎上從形式發送的選項的SQL查詢。在此我想嘗試和共享主WHERE子句聲明的效果與上一個子查詢的WHERE子句。MYSQL子查詢計數WHERE

以例如pass_count,我要計算行,其中total_passfail =「通行證」的數量。這個數字可能會由於主查詢的含義然而改變WHERE函數(內主WHERE函數I有8個可能的選項,例如日期跨度,用戶,組,部門這相應形狀的整體的數據)。

有什麼辦法使整個WHERE子句影響子查詢,而不是我有8個以上哪來添加到每個子查詢。實際上,我只是希望子查詢WHERE子句作用於主查詢返回的數據集之上。

任何幫助將不勝感激。

$query = $this->db->join('users', 'users.staff_id = quality.staff_id') 
->join('user_info', 'user_info.staff_id = users.staff_id') 
->join('teams', 'teams.team_id = users.team_id') 
->join('departments', 'teams.dept_id = departments.dept_id') 
->join('quality_results', 'quality_results.qms_id = quality.qms_id') 
->where('quality.published !=', "") 
->where('quality.published !=', "0000-00-00 00:00:00") 
->select('users.staff_id, user_info.name, user_info.surname, teams.team_name, departments.dept_name, COUNT(quality.qms_id) AS num_assessments, (SELECT (SUM(quality_results.total_result)/COUNT(quality_results.total_result))) AS average_result, (SELECT COUNT(quality_results.total_passfail) FROM quality_results WHERE quality_results.total_passfail = "Pass") AS pass_count'); 

if (strlen($query_array['task_date_from'])) { 
$query->where('quality.task_datetime >=', $query_array['task_date_from']); 
} 
if (strlen($query_array['task_date_to'])) { 
$query->where('quality.task_datetime <=', $query_array['task_date_to']); 
} 
    (+ 4 MORE IF STATEMENTS HERE) 

$result = $query->get('quality'); 

我也厭倦了以下,但得到一個錯誤,而不是替換子查詢:

SUM(IF(quality_results.total_passfail = "Pass", 1,0)) AS pass_count 

這裏是我的原始的SQL代碼:

SELECT `users`.`staff_id`, `user_info`.`name`, `user_info`.`surname`, `teams`.`team_name`, `departments`.`dept_name`, COUNT(quality.qms_id) AS num_assessments, (SELECT (AVG(quality_results.total_result))) AS average_result, (SELECT COUNT(quality_results.total_passfail) FROM quality_results WHERE quality_results.total_passfail = "Pass") AS pass_count FROM (`quality`) JOIN `users` ON `users`.`staff_id` = `quality`.`staff_id` JOIN `user_info` ON `user_info`.`staff_id` = `users`.`staff_id` JOIN `teams` ON `teams`.`team_id` = `users`.`team_id` JOIN `departments` ON `teams`.`dept_id` = `departments`.`dept_id` JOIN `quality_results` ON `quality_results`.`qms_id` = `quality`.`qms_id` JOIN `quality_tasks` ON `quality`.`task_id` = `quality_tasks`.`task_id` WHERE `quality`.`published` != '' AND `quality`.`published` != '0000-00-00 00:00:00' AND `quality`.`task_datetime` >= '2011-04-09' AND `quality`.`task_datetime` <= '2011-05-09' AND `teams`.`dept_id` = '1' 
+0

請發佈整個查詢(不是8,但有1或2個條件在WHERE),所以我們看到你的意思。 – 2011-05-05 22:55:41

+0

現在,我唯一的意見是,你可以使用'AVG()'而不是'SUM()/ COUNT()'。 – 2011-05-05 22:56:16

+0

任何人有任何想法我可以解決這個問題? – 2011-05-07 08:19:12

回答

4

Formatted SQL:

SELECT `users`.`staff_id`, `user_info`.`name`, `user_info`.`surname`, 
     `teams`.`team_name`, `departments`.`dept_name`, 
     COUNT(quality.qms_id) AS num_assessments, 
     (SELECT (AVG(quality_results.total_result))) AS average_result, 
     (SELECT COUNT(quality_results.total_passfail) 
     FROM quality_results 
     WHERE quality_results.total_passfail = "Pass") AS pass_count 
FROM (`quality`) 
JOIN `users` ON `users`.`staff_id` = `quality`.`staff_id` 
JOIN `user_info` ON `user_info`.`staff_id` = `users`.`staff_id` 
JOIN `teams` ON `teams`.`team_id` = `users`.`team_id` 
JOIN `departments` ON `teams`.`dept_id` = `departments`.`dept_id` 
JOIN `quality_results` ON `quality_results`.`qms_id` = `quality`.`qms_id` 
JOIN `quality_tasks` ON `quality`.`task_id` = `quality_tasks`.`task_id` 
WHERE `quality`.`published` != '' 
    AND `quality`.`published` != '0000-00-00 00:00:00' 
    AND `quality`.`task_datetime` >= '2011-04-09' 
    AND `quality`.`task_datetime` <= '2011-05-09' 
    AND `teams`.`dept_id` = '1' 

目前你的pass_count並沒有考慮主要在哪裏,因爲它的子查詢。但是,您可以添加像pass_count左加入quality_results每一列,如下:

SELECT 
    `users`.`staff_id`, `user_info`.`name`, `user_info`.`surname`, `teams`.`team_name`, `departments`.`dept_name`, 
    COUNT(quality.qms_id) AS num_assessments, 
    (SELECT (AVG(quality_results.total_result))) AS average_result, -- It`s a total average result -- as a whole on all system 
    count(quality_results_passed.total_passfail) AS pass_count -- And this will be quality_results, that will take into account your main where 
FROM (`quality`) 
JOIN `users` ON `users`.`staff_id` = `quality`.`staff_id` 
JOIN `user_info` ON `user_info`.`staff_id` = `users`.`staff_id` 
JOIN `teams` ON `teams`.`team_id` = `users`.`team_id` 
JOIN `departments` ON `teams`.`dept_id` = `departments`.`dept_id` 
JOIN `quality_results` ON `quality_results`.`qms_id` = `quality`.`qms_id` 
LEFT JOIN `quality_results` as `quality_results_passed` 
    ON `quality_results_passed`.`qms_id` = `quality`.`qms_id` 
    and quality_results.total_passfail = "Pass" 
JOIN `quality_tasks` ON `quality`.`task_id` = `quality_tasks`.`task_id` 
WHERE `quality`.`published` != '' AND `quality`.`published` != '0000-00-00 00:00:00' 
AND `quality`.`task_datetime` >= '2011-04-09' AND `quality`.`task_datetime` <= '2011-05-09' AND `teams`.`dept_id` = '1' 
+0

完美工作,並在我的uni截止日期前一天,感謝您的幫助:) – 2011-05-09 15:11:37

0

SUM(IF(EXPR, 1, 0))作品就好了。這是我用過的,似乎比嵌套選擇風險低。只需修復你的SQL錯誤。