MySQL查找所有六个月或更长时间没有约会的人
Posted
技术标签:
【中文标题】MySQL查找所有六个月或更长时间没有约会的人【英文标题】:MySQL find all persons who have not had an appointment in six months or more 【发布时间】:2011-05-19 22:28:11 【问题描述】:大家好,我有一个约会表,其中包含一个记录约会日期的 DATE 字段。 show create 语句如下。
| groomappointments | CREATE TABLE `groomappointments` (
`gapmtDate` date NOT NULL,
`gapmtClient` int(11) NOT NULL,
`gapmtUser` int(11) NOT NULL,
`gapmtStatus` int(11) NOT NULL DEFAULT '1',
`gapmtSTime` time NOT NULL,
`gapmtETime` time NOT NULL,
`gapmtPet` int(11) DEFAULT NULL,
`gapmtService` int(11) DEFAULT NULL,
`gapmtTracker` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`gapmtDate`,`gapmtClient`,`gapmtUser`,`gapmtStatus`,`gapmtSTime`),
KEY `gappPet` (`gapmtPet`),
KEY `gappClient` (`gapmtClient`),
KEY `gappSrve` (`gapmtService`),
KEY `gappStat` (`gapmtStatus`),
KEY `gappUsr` (`gapmtUser`),
KEY `gapmtTracker` (`gapmtTracker`),
CONSTRAINT `gappClient` FOREIGN KEY (`gapmtClient`) REFERENCES `clients` (`clientid`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `gappPet` FOREIGN KEY (`gapmtPet`) REFERENCES `pets` (`petID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `gappSrve` FOREIGN KEY (`gapmtService`) REFERENCES `groomservices` (`groomServicesID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `gappStat` FOREIGN KEY (`gapmtStatus`) REFERENCES `aptstatus` (`aptStatusID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `gappUsr` FOREIGN KEY (`gapmtUser`) REFERENCES `users` (`userID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1 |
我正在尝试查询数据库以查找上次约会记录在过去六个月或更长时间的所有客户,但我无法找出正确的查询。
我尝试了以下查询,它将为我提供过去记录 6Mts+ 的所有记录,但包括上周、一个月等有约会的客户。
mysql> select groomappointments.gapmtDate, clients.firstname, clients.lastname
-> from groomappointments,clients
-> WHERE date_sub(CURDATE(), INTERVAL 6 MONTH)>gapmtDate
-> AND clients.clientid = groomappointments.gapmtClient;
非常感谢任何想法。
【问题讨论】:
【参考方案1】:select clients.firstname, clients.lastname, groomappointments.gapmtDate
from clients join groomappointments on clients.clientid = groomappointments.gapmtClient
where clients.clientid in (
select gapmtClient from groomappointments
group by gapmtClient
where date_sub(CURDATE(), INTERVAL 6 MONTH) > gapmtDate
)
【讨论】:
感谢您的回复,但此查询也为我提供了上周有约会的客户。 try ` ... 按具有 min(date_sub(CURDATE(), INTERVAL 6 MONTH)) 的 gapmtClient 分组 > gapmtDate`【参考方案2】:您需要获取最大约会日期,然后查看是否大于 6 个月前,或者根本没有。比如
select appt.lastDate, clients.firstname, clients.lastname
from (select max(gapmtDate) as lastDate,gapmtClient from groomappointments group by gamptClient) as appt ,clients
where date_sub(CURDATE(), INTERVAL 6 MONTH)>lastDate
AND clients.clientid = appt.gapmtClient;
未经测试,但应该可以工作。或者至少给你一个起点。
【讨论】:
感谢您的回复,我会看看这个。 :) 抱歉打错字了,我有阅读障碍,所以我很擅长打错字以上是关于MySQL查找所有六个月或更长时间没有约会的人的主要内容,如果未能解决你的问题,请参考以下文章
Active Directroy user and computers 批量查询被Disable的user 和长时间没有登录的User