我有两张桌子:
accounts
+------+------------------------+
| id | name |
+------+------------------------+
| 1 | John |
| 2 | Lee |
| 3 | Sarah |
| 4 | Michael |
+------+------------------------+
holidays
+------+---------+----------------+--------------+
| id | account | start_date | end_date |
+------+---------+----------------+--------------+
| 1 | 2 | 11/11/11 | 15/11/11 |
+------+---------+----------------+--------------+
这意味着从2011年11月11日至2011年11月15日(含)之间不提供Lee
我需要一个查询,以检查某一天的可用人数。例如,如果我指定日期为13/11/11,Lee将不可用,因此仅检索其他3个帐户,但如果我指定日期为16/11/11,则所有帐户都将可用
答案 0 :(得分:1)
SELECT accounts.*
from accounts
where id not in ( select accounts.id
from accounts
join holidays on accounts.id = holidays.account
where now() between holidays.start_date and holidays.end_date);
这消除了现在有假期的所有用户,即使他们可能还有其他时间(1-n基数)。
答案 1 :(得分:0)
尝试使用此查询:
SELECT name FROM accounts
LEFT JOIN holidays h ON accounts.id = holidays.account
WHERE '20111111' NOT BETWEEN h.start_date AND h.end_date
答案 2 :(得分:0)
那样的东西?
select * from accounts
where id not in (select account
from holiday
where YOUR_DATE between start_date and end_date
)
答案 3 :(得分:0)
尝试这个
SELECT * FROM accounts a
INNER JOIN holidays h
ON a.id=h.account
WHERE '2011-11-13' NOT BETWEEN h.start_date AND h.end_date;