如果我有一个具有colum类别中指定的类别树的表,这是varchar。字符"-“充当类别父-子关系分隔符,但它是字符串的一部分,如下所示(为说明目的简化):
categoryid category
---------- --------
1 Colors
2 Colors-Red
3 Colors-Red-Bright
4 Colors-Red-Medium
5 Colors-Red-Dark
6 Colors-Red-Dark-saturated
7 Colors-Red-Dark-unsaturated
8 Temperatures
9 Temperatures-cold
10 Temperatures-cold-freezing
11 Temperatures-cold-mild
12 Temperatures-hot
13 Temperatures-hot-burning
14 Temperatures-hot-burning-1st degree
15 Temperatures-hot-burning-2nd degree我需要一个查询,它只返回那些没有任何现有“子”类别的类别。因此,此查询只应返回:
categoryid category
---------- --------
3 Colors-Red-Bright
4 Colors-Red-Medium
6 Colors-Red-Dark-saturated
7 Colors-Red-Dark-unsaturated
10 Temperatures-cold-freezing
11 Temperatures-cold-mild
14 Temperatures-hot-burning-1st degree
15 Temperatures-hot-burning-2nd degree有什么办法做到这一点吗?非常感谢!
发布于 2015-08-15 14:03:52
以下是一种方法:
select c.*
from categories c
where not exists (select 1
from categories c2
where c2.category like concat(c.category, '-%')
);发布于 2015-08-15 14:08:20
你可以这样做:
select * from test t
where not exists (
select * from test tt
where tt.id <> t.id AND left(tt.name,length(t.name))=t.name
)这样做的目的是将一个较长的字符串中的所有字符从这个字符串的名称长度以上剪掉,并查看结果是否匹配。
演示。
发布于 2015-08-15 14:15:08
这会管用的。试一次。
SELECT categoryid
, category
FROM TABLE
WHERE category like '%-%-%'https://stackoverflow.com/questions/32025465
复制相似问题