我有一个带有ID的MySQL数据表,两个日期时间和一个布尔列。
看起来像这样:
CREATE TABLE `Example` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Date1` date NOT NULL,
`Date2` date NOT NULL,
`Answered` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
现在我有一个startdate和一个enddate,我需要一个sql查询,它给我行,其中任何 天 之间 startdate , enddate Date1 和 Date2 之间的!
该查询是什么有用的,它给了我那些行, Date1 startdate 和 enddate < / strong>和已解决 = 1 :
$proof = "select * from Example where Date1
between '$startdate' and '$enddate' and Answered = 1";
但我不知道两个日期之间的所有日期如何做到这一点
感谢您的帮助!
答案 0 :(得分:0)
如果我正确理解你想要:
SELECT * FROM Example
WHERE (Date1 BETWEEN '$startdate' AND '$enddate')
AND (Date2 BETWEEN '$startdate' AND '$enddate')
AND Answered = 1
答案 1 :(得分:0)
如果你想包括起点和终点,那么:
SELECT *
FROM `Example`
WHERE `Answered` = 1
AND `Date1` >= '$startdate'
AND `Date2` <= '$enddate'
如果您不想将它们包含在结果中:
SELECT *
FROM `Example`
WHERE `Answered` = 1
AND `Date1` > '$startdate'
AND `Date2` < '$enddate'
你也应该在这里阅读:PHP: Prepared Statements - Manual
答案 2 :(得分:0)
根据上面的2个答案,我解决了我的问题, 这是我需要的查询:
SELECT * FROM `Example`
WHERE ('$startdate' BETWEEN Date1 AND Date2)
OR ('$enddate' BETWEEN Date1 AND Date2)
OR (Date1 BETWEEN '$startdate' AND '$enddate')
OR (Date2 BETWEEN '$startdate' AND '$enddate')
AND Answered = 1
答案 3 :(得分:0)
以下查询将为您提供两个日期之间的确切数据,答案为1
SELECT * FROM `Example`
WHERE ( `Date1` <= '$startdate' and `Date2` >= '$startdate' )
OR ( '$startdate' <= `Date1` and '$enddate' >= `Date1` )
OR ( `Date1` < '$enddate' and `Date2` >= '$enddate' )
AND `Aswered` = 1