id, begin , end msg
1 2 4 A
2 2 4 B
根据 begin 和end 相同,取其中一条
建表SQL语句
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`begin` int(11) DEFAULT NULL,
`end` int(11) DEFAULT NULL,
`msg` int(11) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', '1', '2', '1');
INSERT INTO `test` VALUES ('1', '1', '3', '2');
INSERT INTO `test` VALUES ('2', '2', '31', '3');
INSERT INTO `test` VALUES ('3', '3', '2', '4');
INSERT INTO `test` VALUES ('4', '4', '3', '5');
INSERT INTO `test` VALUES ('5', '5', '5', '6');
3 个解决方案
#1
select * from `test` a where not exists(select 1 from `test`
where a.begin=begin and a.end=end and a.id<id)
where a.begin=begin and a.end=end and a.id<id)
#2
十分感谢大神帮忙
#3
能否用distinct 来做吗
#1
select * from `test` a where not exists(select 1 from `test`
where a.begin=begin and a.end=end and a.id<id)
where a.begin=begin and a.end=end and a.id<id)
#2
十分感谢大神帮忙
#3
能否用distinct 来做吗