mysql找出有重叠时间的数据

有表a如下:

clipboard.png

id为1,2的两条数据,name都是user1,starttime和endtime是有重叠部分的,
id为3,4的两条数据,name都是user2,starttime和endtime是没有重叠部分的。我该怎么写sql找出来id为1,2的数据呢。就是相同用户,开始和结束时间有重叠的。多谢各位大佬~

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  `starttime` datetime DEFAULT NULL,
  `endtime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

BEGIN;
INSERT INTO `test` VALUES (1, 'user1', '2019-07-01 21:53:16', '2019-07-01 22:53:25');
INSERT INTO `test` VALUES (2, 'user1', '2019-07-01 21:23:47', '2019-07-01 22:01:03');
INSERT INTO `test` VALUES (3, 'user2', '2019-07-02 21:54:32', '2019-07-02 22:54:39');
INSERT INTO `test` VALUES (4, 'user2', '2019-07-03 21:54:58', '2019-07-03 22:55:04');
COMMIT;
阅读 4.3k
2 个回答
select
    a.*
from
    test a inner join test b
    on a.name=b.name
where
    a.id<>b.id
    and a.endtime>b.starttime
    and a.starttime<b.endtime
新手上路,请多包涵

完美 感谢!

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题