sql oracle - 区分所有列

sql oracle - distinct all columns

我有以下 2 个表格。我想获取所有模块,其中 fk_class 的第一个字符包含在数字数组中。

ESP_MODULE

moduleId  label
AJ-03     xxxx
PTM-01    yyyy
AN-10     wwww

ESP_MODULE_CLASS

assignId  fk_class fk_module
1           1EM1     AJ-03
2           1EM2     AJ-03
3           2A1      AN-10
4           2A2      AN-10
5           5GC1     AN-10
6           5GC1     PTM-01
7           5GC3     PTM-01

我尝试了以下查询,但得到了重复的值。

查询

select * from ESP_MODULE m inner join ESP_MODULE_CLASS mc ON mc.fk_module = m.moduleId WHERE SUBSTR(mc.fk_class,1,1) IN (1,2);

你只想exists吗?

select m.*
from ESP_MODULE m 
where exists (select 1
              from ESP_MODULE_CLASS mc 
              where mc.fk_module = m.moduleId and
                    SUBSTR(mc.fk_class, 1, 1) IN ('1', '2')
             );

请注意,substr() returns 是一个字符串,因此正确的比较是字符串。您可能会发现该条件更容易写成正则表达式:

where regexp_like(mc.fk_class, '^[12]')