返回条件为

时间:2017-01-25 05:58:02

标签: php mysql codeigniter codeigniter-3

我有一张这样的表

allotment

allotment_id | date | room_id | is_closed
-----------------------------------------
1 | 2017-01-28 | 21 | 0
2 | 2017-01-29 | 21 | 1
3 | 2017-01-30 | 21 | 0
4 | 2017-01-31 | 21 | 0
5 | 2017-01-28 | 32 | 0
6 | 2017-01-29 | 32 | 0
7 | 2017-01-30 | 32 | 0
8 | 2017-01-31 | 32 | 0

我尝试这样的查询:

SELECT *
FROM allotment
WHERE date BETWEEN '2017-01-28' AND DATE_SUB('2017-01-31', INTERVAL 1 DAY)
AND is_closed = 0
AND date >= '2017-01-28' AND l.`date` <= DATE_SUB('2017-01-31', INTERVAL 1 DAY) AND allotment_id NOT IN (select allotment_id FROM allotment WHERE is_closed = 1 AND date >= '2017-01-28' AND date <= '2017-01-31')

如您所见,我从2017-01-28至2017-01-31之间选择,并且在 room_id 21日期2017-01-29 的桌子上有两个房间ID is_closed 是1。

我想要的只是 room_id 32,因为在 room_id 21中我选择的日期范围是 is_closed 中的值1。

我已经尝试了我的查询,但它没有工作。我怎么能这样做。

谢谢。

2 个答案:

答案 0 :(得分:1)

在哪里和子查询中你已经采取了allotment_id。而不是这个你必须占用房间ID

SELECT *
FROM allotment
WHERE date BETWEEN '2017-01-28' AND DATE_SUB('2017-01-31', INTERVAL 1 DAY)
AND is_closed = 0 and room_id not in (select room_id FROM allotment WHERE is_closed = 1 AND date >= '2017-01-28' AND date <= '2017-01-31')

答案 1 :(得分:0)

按房间分组,只拍摄$(document).ready(function(){ $('button').click(function(){ var id = $(this).attr('id'); $.ajax({ url: 'DB/tableDisplay.php', type: 'POST', data: {id: id}, dataType: 'html' }) }); }); 旗帜的零次

is_closed