sql fiddle 不起作用,所以我将在此处添加代码。
CREATE TABLE `TableTT1` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`expiry` date NOT NULL,
`name` varchar(16) NOT NULL,
`frame_id` int(11) DEFAULT NULL,
`component_id` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
INSERT INTO `TableTT1` (`expiry`, `name`, `frame_id`, `component_id`)
VALUES
('2013-10-03', 'component_1', 0320, 127),
('2013-11-12', 'component_1', 0275, 461),
('2013-11-22', 'component_4', 0320, 011),
('2014-01-05', 'component_2', null, 224),
('2014-01-16', 'component_3', 0275, 782);
SELECT * FROM TableTT1;
+----+------------+-------------+----------+--------------+
| ID | expiry | name | frame_id | component_id |
+----+------------+-------------+----------+--------------+
| 1 | 2013-10-03 | component_1 | 320 | 127 |
| 2 | 2013-11-12 | component_1 | 275 | 461 |
| 3 | 2013-11-22 | component_4 | 320 | 11 |
| 4 | 2014-01-05 | component_2 | NULL | 224 |
| 5 | 2014-01-16 | component_3 | 275 | 782 |
+----+------------+-------------+----------+--------------+
SELECT
MIN(expiry) AS expFirst,
GROUP_CONCAT(frame_id, ' ', expiry, ' ', name, ' ', component_id) AS Details
FROM TableTT1
GROUP BY frame_id, name
ORDER BY expFirst;
+------------+--------------------------------+
| expFirst | Details |
+------------+--------------------------------+
| 2013-10-03 | 320 2013-10-03 component_1 127 |
| 2013-11-12 | 275 2013-11-12 component_1 461 |
| 2013-11-22 | 320 2013-11-22 component_4 11 |
| 2014-01-05 | NULL |
| 2014-01-16 | 275 2014-01-16 component_3 782 |
+------------+--------------------------------+
希望这可以帮助。