如果 field free 是 bool (tinyInt),你可以尝试这样的事情:
SELECT * FROM (
SELECT `hotelName`, MIN(`available`) AS _available FROM `hotels` WHERE dbdate BETWEEN 20130507 AND 20130511 GROUP BY `hotelName`)a
WHERE _available;
上面的查询给出了测试结果中的两家酒店,当您将 20130507 更改为 20130503 时,它只会返回阿姆斯特丹酒店。
以此作为测试表:
CREATE TABLE `hotels` (
`hotelName` varchar(20) DEFAULT NULL,
`dbdate` int(11) DEFAULT NULL,
`available` tinyint(1) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*Data for the table `hotels` */
insert into `hotels`(`hotelName`,`dbdate`,`available`) values ('Amsterdam Hotel',20130503,1),('Amsterdam Hotel',20130504,1),('Amsterdam Hotel',20130505,1),('Amsterdam Hotel',20130506,1),('Amsterdam Hotel',20130507,1),('Amsterdam Hotel',20130508,1),('Amsterdam Hotel',20130509,1),('Amsterdam Hotel',201305010,1),('Amsterdam Hotel',201305011,1),('Amsterdam Hotel',201305012,1),('Utrecht Hotel',20130503,0),('Utrecht Hotel',20130504,0),('Utrecht Hotel',20130505,0),('Utrecht Hotel',20130506,0),('Utrecht Hotel',20130507,1),('Utrecht Hotel',20130508,1),('Utrecht Hotel',20130509,1),('Utrecht Hotel',201305010,1),('Utrecht Hotel',201305011,1),('Utrecht Hotel',201305012,0);