Divante Posted April 26, 2011 Share Posted April 26, 2011 ---------+----------------------------------------------------- Table | Create Table ---------+----------------------------------------------------- records | CREATE TABLE `records` ( `id` int(11) NOT NULL AUTO_INCREMENT, `User_id` int(11) NOT NULL DEFAULT '0', `First_Name` varchar(20) DEFAULT NULL, `Last_Name` varchar(25) DEFAULT NULL, `Product` enum('flowers', 'vases', 'ornaments', 'draperies' , `Create_Date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ENGINE=MyISAM DEFAULT CHARSET=latin1 | Basically im stumped on how to get the user id from the table but only for those users whose max create date within the range of 3month and a month ago from the current time.That is i only want records for those users whose last purchase was a month nad up to 3 months ago.Any kind of help will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/234801-mysql-date-time-select/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2011 Share Posted April 27, 2011 SELECT User_id, MAX(Create_Date) as max_date FROM records GROUP BY User_id HAVING DATE(max_date) BETWEEN DATE_SUB(CURDATE(),INTERVAL 3 MONTH) AND DATE_SUB(CURDATE(),INTERVAL 1 MONTH) Quote Link to comment https://forums.phpfreaks.com/topic/234801-mysql-date-time-select/#findComment-1206668 Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2011 Share Posted April 27, 2011 You can make the above query a little more efficient by throwing in a WHERE clause so that only records greater-than or equal to three months ago are used. Quote Link to comment https://forums.phpfreaks.com/topic/234801-mysql-date-time-select/#findComment-1206674 Share on other sites More sharing options...
Divante Posted April 27, 2011 Author Share Posted April 27, 2011 Thanks very much exactly what i needed. Quote Link to comment https://forums.phpfreaks.com/topic/234801-mysql-date-time-select/#findComment-1207278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.