Hi all I have an appointments table that has among other fields a DATE field that records the date of the appointment. The show create statement is below.
| 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 |
I’m trying to query the database to find all clients whose last appointment was recorded six months or more in the past, but I can not figure out the correct query.
I’ve tried the following query which will give me all records that are recorded 6Mts+ in the past but includes clients who have had appointments in the last week, month etc.
mysql> select groomappointments.gapmtDate, clients.firstname, clients.lastname
-> from groomappointments,clients
-> WHERE date_sub(CURDATE(), INTERVAL 6 MONTH)>gapmtDate
-> AND clients.clientid = groomappointments.gapmtClient;
Any ideas greatly appreciated.
1 Answer