在范围SQL中查找重复项

时间:2016-01-19 23:52:39

标签: mysql sql

我有一些包含大量数字数据的表格: 例如,对于其中一个表我有这样的值

    CREATE TABLE `mdacardrange` (
  `rID` decimal(20,0) NOT NULL DEFAULT '0',
 ,
  `range_begin` varchar(254) DEFAULT NULL,
  `range_end` varchar(254) DEFAULT NULL,

  PRIMARY KEY (`rID`),

);

INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (1,'4567890000000000000','4567899999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (2,'5264800000000000000','5264809999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (3,'5193060000000000000','5193069999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (4,'5482150000000000000','5482159999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (5,'5416710000000000000','5416719999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (6,'5167340000000000000','5167349999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (7,'4043090000000000000','4043099999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (8,'4043110000000000000','4043119999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (9,'4226060000000000000','4226069999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (10,'4043100000000000000','4043109999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (11,'4063710000000000000','4063719999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (12,'4098050000000000000','4098059999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (15,'4779040000000000000','4779049999999999999');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (16,'5223050000000000000','5223059999999999999');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (17,'4703770000000000000','4703779999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (18,'5483930000000000000','5483939999999999000');
INSERT INTO `mdacardrange` (`rID`,`range_begin`,`range_end`) VALUES (19,'5341480000000000000','5341489999999999999');

我的任务是比较重复项的数据范围。不幸的是,我甚至不知道如何处理这些数据,据我所知,不知怎的,我需要计算每一行的整个范围,然后进行比较,有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您的CREATE TABLE包含一些拼写错误......

我认为这样的事情应该有效:

SQL> select 
         a.*, b.rID as overlapped_id 
     from 
         mdacardrange a inner join mdacardrange b on 
             a.range_begin < b.range_end and 
             a.range_end > b.range_begin and 
             a.rID <> b.rID 
     ;