 |
อยากให้ group by ข้อมูลแล้วให้ได้ record ล่้าสุด แทนที่จะเป็น record แรก หรือมีวิธีอื่นไหมครับ |
|
 |
|
|
 |
 |
|
ดันด้วยคนครับ ผมก็ต้องการเป็นอย่างมาก
|
 |
 |
 |
 |
Date :
2012-02-10 23:39:09 |
By :
อยากรู่เหมือนกัน |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code
CREATE TABLE `customer` (
`custid` int(3) unsigned NOT NULL,
`name` varchar(50) collate utf8_unicode_ci default NULL,
`datesale` date default NULL,
KEY `custid` (`custid`),
KEY `name` (`name`),
KEY `datesale` (`datesale`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `customer`
--
INSERT INTO `customer` VALUES (1, 'poh', '2012-01-25');
INSERT INTO `customer` VALUES (1, 'poh', '2012-02-11');
INSERT INTO `customer` VALUES (3, 'Yo', '2012-01-21');
INSERT INTO `customer` VALUES (1, 'poh', '2012-02-05');
INSERT INTO `customer` VALUES (2, 'aoh', '2012-01-15');
INSERT INTO `customer` VALUES (1, 'poh', '2012-01-31');
INSERT INTO `customer` VALUES (3, 'Yo', '2011-11-21');
INSERT INTO `customer` VALUES (2, 'aoh', '2012-01-01');
INSERT INTO `customer` VALUES (3, 'Yo', '2012-03-11');
ง่ายๆไม่ยากครับ
SELECT `custid` as refid ,`name`,`datesale` FROM `customer` as t1
where datesale = (select max(datesale) from customer where custid=refid)
order by `name`
ผลลัพธ์
2 aoh 2012-01-15
1 poh 2012-02-11
3 Yo 2012-03-11
|
 |
 |
 |
 |
Date :
2012-02-11 02:51:39 |
By :
Unidentier |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ได้จริง ๆ ขอบคุณมากๆ ครับ
|
 |
 |
 |
 |
Date :
2012-02-12 10:44:01 |
By :
Laa |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|