How to get latest date from fees table and compare it to current date in mysql I have two tables members and fees ,fees table has a foreign key memberid which is a primary key in members .
I want to get those members who have not yet paid. When a member pays there are two fields fromdate and todate and both they are datetime.
So those fields helps us to know that the member paid from which date to which date .
So I want to pull out those members whose todate is older than the current date , remember since they pay monthly I want to pull out only 
one latest todate  for each member  then I compare it with current date . Any help will be appreciated . bellow is what I have tried . 

 select  members.name , fees.todate ,
    fees.usercode
    from members 
    INNER JOIN fees
    ON members.memberid  =  fees.memberid
    WHERE
     fees.usercode ='KMTS472895'
    AND 
    fees.todate &lt; curdate() 
    AND 
    fees.todate = (SELECT MAX(fees.todate))
    AND 
    todate !='0000-00-00 00:00:00'
    GROUP BY fees.memberid
    ORDER BY fees.memberid


///////////////////////////////////////////

CREATE TABLE `members` (
  `memberid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `companyid` int(11) unsigned DEFAULT NULL,
  `name` varchar(30) NOT NULL,
  `initials` varchar(5) NOT NULL,
  `surname` varchar(30) NOT NULL,
  `datejoined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `usercode` varchar(12) NOT NULL,
  `address` varchar(50) DEFAULT NULL,
  `cell` varchar(15) DEFAULT NULL,
  `cell2` varchar(15) DEFAULT NULL,
  `pic` varchar(100) DEFAULT NULL,
  `idnumber` varchar(30) DEFAULT NULL,
  `rank` varchar(30) DEFAULT NULL,
  `gender` varchar(12) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `address2` varchar(50) DEFAULT NULL,
  `branch` varchar(50) DEFAULT NULL,
  `dob` date DEFAULT NULL,
  `Company_name` varchar(100) DEFAULT NULL,
  `website` varchar(100) DEFAULT NULL,
  `Comments` varchar(500) DEFAULT NULL,
  `Company_reg_number` varchar(50) DEFAULT NULL,
  `country` varchar(100) DEFAULT NULL,
  `city` varchar(100) DEFAULT NULL,
  `next_of_kin` varchar(100) DEFAULT NULL,
  `next_of_kin_number` varchar(100) DEFAULT NULL,
  `patient_code` varchar(50) DEFAULT NULL,
  `medNumber` varchar(100) DEFAULT NULL,
  `medPlan` varbinary(100) DEFAULT NULL,
  PRIMARY KEY (`memberid`),
  KEY `companyid` (`companyid`),
  CONSTRAINT `members_ibfk_1` FOREIGN KEY (`companyid`) REFERENCES `company` (`companyid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1;

///////////////////////////////////////////////////



CREATE TABLE IF NOT EXISTS `fees` (
  `feesid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `feestype` varchar(50) NOT NULL,
  `amountpaid` decimal(10,2) DEFAULT NULL,
  `memberid` int(11) unsigned DEFAULT NULL,
  `companyid` int(11) unsigned DEFAULT NULL,
  `paidfor` varchar(100) NOT NULL,
  `paiddate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `fromdate` datetime NOT NULL,
  `todate` datetime NOT NULL,
  `usercode` varchar(12) NOT NULL,
  `paidby` varchar(30) NOT NULL,
  `recieved_by` varchar(30) NOT NULL,
  `qty` int(5) NOT NULL,
  PRIMARY KEY (`feesid`),
  KEY `memberid` (`memberid`),
  KEY `companyid` (`companyid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1595 ;
Try this

select  members.name , fees.todate ,
fees.usercode
from members
INNER JOIN 
(
select max(usercode) as usercode
, memberid
, max(todate) as todate
from
fees    
WHERE
 fees.usercode ='KMTS472895'
 GROUP BY fees.memberid
) as fees
on members.memberid = fees.memberid
where fees.todate &lt; curdate()
order by members.memberid