您好我有两个日期from
和to
的文本框。当我在两个文本框中选择日期,表示时间间隔时,我想填写缺少的日期,这样我就可以在表中查询一些记录以及假设我有
2014-08-02
2014-08-08
2014-08-10
2014-08-11
2014-08-07
2014-08-12
或者可能是另一个约会,请帮帮我
<?php
$from='2014-08-11';
$to='2014-09-10';
$query="SELECT * FROM stats WHERE Feeddate BETWEEN '2014-08-11' AND '2014-09-10'";
?>
答案 0 :(得分:1)
$from = '2014-08-11';
$to = '2014-09-10';
$query = "SELECT Feeddate FROM stats WHERE Feeddate BETWEEN '$from' AND '$to' ORDER BY Feeddate";
$cursor = DateTime::createFromFormat('Y-m-d', $from);
$res = mysql_query($query);
while ($row = mysql_fetch_assoc($res))
{
$date = DateTime::createFromFormat('Y-m-d', $row['Feeddate']);
while ($cursor != $date)
{
echo "Day is missing: " . $cursor->format('Y-m-d') . "<br>";
$cursor->modify('+1 day');
}
$cursor->modify('+1 day');
}
答案 1 :(得分:1)
没有测试它,但是这样的东西。您需要在要查找的范围内创建一系列日期的临时表。例如2014-08-11至2014-09-10。这有一种方法:
然后你想要查询
SELECT *
FROM x
(
SELECT date
FROM dateseries
WHERE date BETWEEN '2014-08-11' AND '2014-09-10'
) as x
WHERE NOT IN (
SELECT date
FROM stats
WHERE Feeddate BETWEEN '2014-08-11' AND '2014-09-10'
);
希望你能得到这个想法。只要知道MySql在子查询优化中糟透了,所以您可能希望将该子查询移动到连接。