我尝试获取用户的离开报告,我想在两个范围之间获取数据,以及离开日期是否在所选范围之间。 例如 这是我的数据库 假设我选择了2017-06-13至2017-06-17的范围,那么应选择第二排,假设我选择了2017-06-21至2017-06-25的范围,那么它也应该被选中我试过在两个不同的查询下面得到相同的结果
(a)中
(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();
提前致谢。
答案 0 :(得分: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();