PDA

View Full Version : mySql query help



pablowerk
09-11-2008, 02:57 PM
I have this table,

CREATE TABLE `paw_tags_master_status` (
`paw_tags_master_statusid` int(11) NOT NULL auto_increment,
`paw_tags_masterlistid` int(11) NOT NULL,
`paw_tags_statustypeid` int(11) NOT NULL,
`adddate` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`paw_tags_master_statusid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;

--
-- Dumping data for table `paw_tags_master_status`
--

INSERT INTO `paw_tags_master_status` (`paw_tags_master_statusid`, `paw_tags_masterlistid`, `paw_tags_statustypeid`, `adddate`) VALUES
(1, 1, 2, '2008-09-04 16:21:13'),
(2, 901, 2, '2008-09-04 16:22:24'),
(3, 902, 2, '2008-09-04 16:23:41'),
(9, 1, 3, '2008-09-05 10:02:02'),
(10, 2, 2, '2008-09-05 10:05:15'),
(11, 2, 3, '2008-09-05 10:05:45'),
(12, 3, 2, '2008-09-05 12:29:42'),
(13, 3, 3, '2008-09-05 12:30:39'),
(14, 445, 2, '2008-09-09 20:17:59'),
(15, 445, 3, '2008-09-09 20:18:11'),
(16, 556, 2, '2008-09-09 22:36:02'),
(17, 556, 3, '2008-09-09 22:37:41'),
(18, 113, 2, '2008-09-11 09:43:56'),
(19, 113, 3, '2008-09-11 09:45:52'),
(20, 123, 2, '2008-09-11 10:10:27'),
(21, 123, 3, '2008-09-11 10:10:59'),
(22, 125, 2, '2008-09-11 10:12:09'),
(23, 125, 3, '2008-09-11 10:12:28'),
(24, 126, 2, '2008-09-11 10:17:43'),
(25, 126, 3, '2008-09-11 10:17:55'),
(26, 127, 2, '2008-09-11 10:21:46'),
(27, 127, 3, '2008-09-11 10:22:07');


I am trying to create a query that would output the distinct `paw_tags_masterlistid` values along with their most recent `paw_tags_statustypeid` value

I am trying to get the results back to look like this using one query.

paw_tags_master_statusid paw_tags_masterlistid paw_tags_statustypeid adddate
9 1 3 2008-09-05 10:02:02
2 901 2 2008-09-04 16:22:24
3 902 2 2008-09-04 16:23:41
11 2 3 2008-09-05 10:05:45


Any help is greatly appreciated.

wige
03-17-2009, 09:26 AM
SELECT paw_tags_masterlistid, paw_tags_statustypeid FROM paw_tags_master_status GROUP BY paw_tags_masterlistid ORDER BY adddate;

I would add an output of the adddate field just to make sure it is showing the latest and not the earliest date. You may need to change the order by to ASC or DESC, I don't remember which way the default sort goes.