Codeigniter - 选择id不在的位置(另一个查询结果)

时间:2016-06-23 02:29:40

标签: php mysql codeigniter

我正在进入酒店预订系统,目前我正在尝试选择可用房间(未预订)。

Rooms DB Structure:
ID
ROOM NAME
CAPACITY

HOTEL RESERVATIONS DB STRUCTURE:
ID
CHECK_IN
CHECK_OUT
ROOMS
...

这是我目前的代码:

function searchFreeRooms($data){
  $check_in = $data['fields']['check_in'];
  $check_out = $data['fields']['check_out'];
  $this->db->select("*");
  $this->db->from('core_hotel_rooms');
  $this->db->where("id NOT IN (select rooms,total_guests from res_hotel where check_in <= '$check_in' AND check_out >= '$check_in' OR check_in <= '$check_out' AND check_out >= '$check_out' OR check_in >= '$check_in' AND check_out <= '$check_out' ) ");
  $query = $this->db->get();
  return $query->result();
 }

用户可以一次预订多个房间,预留的房间ID存储在&#34; ROOMS&#34;用逗号分隔:2,3,5

在我的前面,不应该显示此列中存在的房间,但我遇到了麻烦,因为只选择了逗号之前的第一个id(房间),例如:2,3,5&gt;只选择了2,我的前面仍然显示3,5。

问题在于:$this->db->where("id NOT IN (select rooms,total_guests from res_hotel where check_in <= '$check_in' AND check_out >= '$check_in' OR check_in <= '$check_out' AND check_out >= '$check_out' OR check_in >= '$check_in' AND check_out <= '$check_out' ) ");

我试过这个:$this->db->where("id NOT IN (1, 2) ");它完美无缺,但不是第二个查询的上方法。

抱歉我的英文......

非常感谢所有能提供帮助的人!

4 个答案:

答案 0 :(得分:5)

最后,在你的帮助下,我已经解决了我的问题!

工作代码:

function searchFreeRooms($data){
  $check_in = $data['fields']['check_in'];
  $check_out = $data['fields']['check_out'];
  $query1 = $this->db->query("select rooms from res_hotel where (check_in <= '$check_in' AND check_out >= '$check_in') OR (check_in <= '$check_out' AND check_out >= '$check_out') OR (check_in >= '$check_in' AND check_out <= '$check_out' )");
  $query1_result = $query1->result();
  $room_id= array();
  foreach($query1_result as $row){
     $room_id[] = $row->rooms;
   }
  $room = implode(",",$room_id);
  $ids = explode(",", $room);
  $this->db->select("*");
  $this->db->from('core_hotel_rooms');
  $this->db->where_not_in('id', $ids);
  $query = $this->db->get();
  return $query->result();
 }

非常感谢!

答案 1 :(得分:1)

考虑使用括号

   $this->db->where("id NOT IN (select rooms,total_guests from res_hotel where (check_in <= '$check_in' AND check_out >= '$check_in') OR (check_in <= '$check_out' AND check_out >= '$check_out') OR (check_in >= '$check_in' AND check_out <= '$check_out' ) ) ");

答案 2 :(得分:0)

以下是您的问题的解决方案。希望这有帮助:

function searchFreeRooms($data){
  $check_in = $data['fields']['check_in'];
  $check_out = $data['fields']['check_out'];

  $query1 = $this->db->query("select rooms from res_hotel where check_in <= '".$check_in."' AND check_out >= '".$check_in."' OR check_in <= '".$check_out."' AND check_out >= '".$check_out."' OR check_in >= '".$check_in."' AND check_out <= '".$check_out."'")->result_array();
  $room_id= array();
  foreach($query1 as $row){
     $room_id[] = $row->rooms;
   }
  $room = implode(",",$room_id);

  $query = $this->db->query("select * from rooms where id not in (".$room.")");

      return $query->result();
     }

答案 3 :(得分:0)

$result = $this->db->select('room')
     ->get('your_table');

    foreach($result as $item) {
        $array[] = $item['id'];         
    }
    $a = implode(',', $array);

$this->where_not_in('id', $a);

做这样的事情。