[英]Get rows where value is not a substring in another row
我正在针对包含循环引用的表编写递归sql。 没问题! 我读到您可以构建一条唯一的路径来防止无限循环。 现在,我需要将列表筛选到仅链中的最后一条记录。 不过我一定做错了。 -edit我将更多记录添加到此示例中,以更清楚为什么仅选择最长的记录不起作用。
这是一个示例表:
create table strings (id int, string varchar(200));
insert into strings values (1, '1');
insert into strings values (2, '1,2');
insert into strings values (3, '1,2,3');
insert into strings values (4, '1,2,3,4');
insert into strings values (5, '5');
而我的查询:
select * from strings str1 where not exists
(
select * from strings str2
where str2.id <> str1.id
and str1.string || '%' like str2.string
)
我希望只得到最后的记录
| id | string |
|----|---------|
| 4 | 1,2,3,4 |
| 5 | 5 |
相反,我全都得到了
| id | string |
|----|---------|
| 1 | 1 |
| 2 | 1,2 |
| 3 | 1,2,3 |
| 4 | 1,2,3,4 |
| 5 | 5 |
链接到SQL Fiddle: http ://sqlfiddle.com/#!15/7a974/1
我的问题是围绕“喜欢”比较。
select * from strings str1
where not exists
(
select
*
from
strings str2
where
str2.id <> str1.id
and str2.string like str1.string || '%'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.