[英]Check range date in MySQL
I have in my database MySQL: 我的数据库中有MySQL:
id | text | date_start | date_stop
1 | aa | 2011-12-17 10:45:00 | 2011-12-18 15:33:00
2 | aa | 2011-12-18 19:11:44 | 2011-12-20 10:10:19
3 | aa | 2011-12-21 15:14:00 | 2011-12-23 11:21:00
and i want check if example date is in database. 我想检查示例日期是否在数据库中。 for example i im giving:
例如我给:
date_start: 2011-12-17 11:11:11 and date_stop 2011-12-18 16:11:11
this should return false. 这应该返回false。
date_start: 2011-12-20 11:11:11 and date_stop 2011-12-21 11:11:11
this should return true. 这应该返回true。
how can i make this? 我该怎么做? which query should be for this in SQL?
在SQL中应该针对哪个查询?
WHERE start_date BETWEEN $start AND $end
OR end_date BETWEEN $start AND $end
OR ($start < start_date AND $end > end_date)
if you have a result, its false, else true. 如果有结果,则为假,否则为真。
Try this: 尝试这个:
SELECT COUNT(*)>0 FROM tbl WHERE date_start BETWEEN '2011-12-20 11:11:11' AND '2011-12-21 11:11:11'
I suppose that date_start
should be between given dates. 我想
date_start
应该在给定的日期之间。
Not sure if this is what you mean, but: 不知道这是否是您的意思,但是:
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table WHERE date_start = '2011-12-17 11:11:11' AND date_stop = '2011-12-18 16:11:11'", $link);
$amount = mysql_num_rows($result);
if($amount > 0)
{
return true;
}
else
{
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.