I have items list. Every single item may be booked for specific date, eg. 2013-01-03 00:00:00, 2013-06-13 00:00:00 etc. I have to filter items in way that shows available items for choosen date. If item have only single reservation there is no problem, but I don't know hot to construct query if item have a few reservations
cms_items
[id:int, name:varchar, category:int]
cms_items_reservations
[id:int,item_id:int,reservation_date:timestamp]
I use CodeIgniter and I created something like this:
$this->db->
select('DISTINCT(item.item_id), item.name, item.category', false)->
from('cms_items_reservations AS rs')->
join('cms_items AS item', 'item.id = rs.item_id', 'right')->
where('item.category', $regionId)->
where('DATE_FORMAT(rs.reservation_date, "%Y-%m-%d") <>', $date)->
where('rs.item_id = item.id');
If item is reserved for 2013-06-13 and 2013-06-14 and I'm looking for items available for 2013-06-13 it's still returned with this query.
I don't want. I'm looking for most simpliest way. Any advices?
You can use a simple subquery
like
SELECT * FROM cms_items WHERE id NOT IN (
SELECT item_id FROM cms_items_reservations WHERE
DATE_FORMAT(reservation_date, "%Y-%m-%d") ='2013-06-13')
$this->db->query("SELECT * FROM cms_items WHERE id NOT IN (
SELECT item_id FROM cms_items_reservations WHERE
DATE_FORMAT(reservation_date, '%Y-%m-%d') ='".$date."')");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.