防止SQL查询中出现冗余结果
我对查找时间冲突的查询如下所示-防止SQL查询中出现冗余结果,sql,Sql,我对查找时间冲突的查询如下所示- create table dat(id int, s time(7), e time(7)); insert into dat (id,s,e) values(1,'16:00:00.0000000','18:00:00.0000000') insert into dat (id,s,e) values(2,'15:00:00.0000000','17:00:00.0000000') insert into dat (id,s,e) values(3,'12:
create table dat(id int, s time(7), e time(7));
insert into dat (id,s,e) values(1,'16:00:00.0000000','18:00:00.0000000')
insert into dat (id,s,e) values(2,'15:00:00.0000000','17:00:00.0000000')
insert into dat (id,s,e) values(3,'12:00:00.0000000','15:00:00.0000000')
//data part
QUERY -
select * from dat a, dat b
where a.id != b.id
and a.s < b.e and a.e > b.s
创建表dat(id int,s时间(7),e时间(7));
在dat(id,s,e)值中插入(1,'16:00:00.0000000','18:00:00.0000000')
在dat(id,s,e)值中插入(2,'15:00:00.0000000','17:00:00.0000000')
在dat(id、s、e)值中插入(3、'12:00:00.0000000'、'15:00:00.0000000')
//数据部分
查询-
从数据a、数据b中选择*
a.id!=b、 身份证
a.sb.s
结果-
现在的问题是查询列表-
在第一行中,id 1与id 2冲突
在第二行中,id 2与id 1冲突
我需要这个问题的明确答案。请建议。试试:
SELECT * from dat a, dat b
WHERE a.id < b.id
AND a.s < b.e
AND a.e > b.s
从数据a、数据b中选择*
其中a.idb.s
您可以通过将第一个id设置为低于第二个id来避免重复对。您使用的是什么DBMS?虽然我无法帮助您使用MSSQL特定的函数,但通过
通过a.id
和array\u agg
ingb.id
对进行分组,您可能会获得良好的效果。像这样,每个a
都有一个完全重叠的b
,没有重复项。谢谢它在这里起作用。虽然我不知道它在我原来的场景中是否起作用。无论如何,谢谢