2017-06-13 118 views
0

我嘗試獲取用戶的離開報告,我想要在兩個範圍之間獲取數據,並且葉子日期位於所選範圍之間。 例如 這裏是我的分數 enter image description here 假設我選擇的範圍從2017-06-13到2017-06-17,那麼應該選擇第二行,並且假設我選擇的範圍從2017-06-21到2017-06- 25那麼也應該選我曾嘗試以下兩種不同的查詢來獲得同樣的結果從兩個範圍的數據庫中提取數據?

(一)

(SELECT u.user_id, u.user_image, u.user_first_name, u.user_last_name, CONCAT(u.user_first_name, ' ', u.user_last_name) AS user_name, 
     u.user_email, p.driver_number, l.status, DATE_FORMAT(l.from_date, '%d-%m-%Y') as from_date, DATE_FORMAT(l.to_date, '%d-%m-%Y') as to_date 
     FROM (`bat_usermst` u) 
     JOIN `bat_leavemst` l ON `u`.`user_id` = `l`.`user_id` 
     JOIN `bat_profilemst` p ON `u`.`user_id` = `p`.`user_id` 
     WHERE `l`.`from_date` >= '" . $start . "' AND `l`.`from_date` <= '" . $end . "' AND 
     `l`.`status` = '1' AND `u`.`user_type` = 5 AND `u`.`user_status` = '1' AND `u`.`is_deleted` = '0' 
     ORDER BY `user_name`) 

     UNION 

     (SELECT u.user_id, u.user_image, u.user_first_name, u.user_last_name, CONCAT(u.user_first_name, ' ', u.user_last_name) AS user_name, 
     u.user_email, p.driver_number, l.status, DATE_FORMAT(l.from_date, '%d-%m-%Y') as from_date, DATE_FORMAT(l.to_date, '%d-%m-%Y') as to_date 
     FROM (`bat_usermst` u) 
     JOIN `bat_leavemst` l ON `u`.`user_id` = `l`.`user_id` 
     JOIN `bat_profilemst` p ON `u`.`user_id` = `p`.`user_id` 
     WHERE `l`.`to_date` >= '" . $start . "' AND `l`.`to_date` <= '" . $end . "' AND 
     `l`.`status` = '1' AND `u`.`user_type` = 5 AND `u`.`user_status` = '1' AND `u`.`is_deleted` = '0' 
     ORDER BY `user_name`) 
     "; 

(b)

$this->db->select("u.user_id, u.user_image, u.user_first_name, u.user_last_name, CONCAT(u.user_first_name,' ', u.user_last_name) AS user_name,u.user_email, p.driver_number,l.status, 
     DATE_FORMAT(l.from_date, '%d-%m-%Y') as from_date, 
     DATE_FORMAT(l.to_date, '%d-%m-%Y') as to_date", FALSE); 

     $this->db->from('usermst u'); 
     $this->db->join('bat_leavemst l', 'u.user_id = l.user_id'); 
     $this->db->join('profilemst p', 'u.user_id = p.user_id'); 
     $this->db->where('l.from_date >=', "$start"); 
     $this->db->where('l.to_date <=', "$end"); 
     $this->db->where('l.status', '1'); 
     $this->db->where(array('u.user_type' => DRIVER, 'u.user_status' => '1', 'u.is_deleted' => '0')); 

     $this->db->order_by('user_name'); 
     $result = $this->db->get(); 

感謝進階ANCE。

+0

請幫自己一個忙,並澄清(a)顯示樣品的查詢詞,你想獲取和(b)與基於文本的表替換圖像的行,所以我們可以看到它你的問題。這可能是你的英語,但我不知道你想要你的查詢做什麼。 – alexis

+0

你爲什麼要結合?你可以用簡單的'OR'條件來做到這一點。 – jagad89

回答

0

正如我可以看到你的聯合查詢只有一個區別在哪裏查詢的一部分。 通常,只要你有複雜的where條件,你就可以在MySQL中簡單地指定這些條件。下面的代碼應該給出所需的結果。

$where = "((`l`.`from_date` >= '" . $start . "' AND `l`.`from_date` <= '" . $end . "') ". 
     "OR (`l`.`to_date` >= '" . $start . "' AND `l`.`to_date` <= '" . $end . "')) ". 
      "AND `l`.`status` = '1' AND `u`.`user_type` = 5 AND `u`.`user_status` = '1' AND `u`.`is_deleted` = '0'"; 
     $this->db->from('usermst u'); 
     $this->db->join('bat_leavemst l', 'u.user_id = l.user_id'); 
     $this->db->join('profilemst p', 'u.user_id = p.user_id'); 
     $this->db->where($where,NULL,FALSE); 
     $this->db->order_by('user_name'); 
     $result = $this->db->get();