简体   繁体   English

选择不符合条件的行

[英]Select rows which do not meets criteria

I have FEATURE and FEATURE_DETAILS tables. 我有FEATURE和FEATURE_DETAILS表。 FEATURE_DETAILS can have a lot of FEATUREs. FEATURE_DETAILS可以有很多功能。

FEATURE_DETAILS
feature_deatails_id | feature_id
1                        1
1                        2
1                        4 
2                        1
2                        2
2                        4
2                        5

I have a problem in selecting feature_deatails_id's which do not have eg 5 feature_id. 我在选择没有5个feature_id的feature_deatails_id时遇到问题。 So the result should be: 因此结果应为:

feature_deatails_id | feature_id
1                     null

This should solve your purpose 这应该解决您的目的

with data as (
          select 1 as feature_details_id, 1 feature_id from dual
    union select 1,2 from dual
    union select 1,4 from dual
    union select 2,1 from dual
    union select 2,2 from dual
    union select 2,4 from dual
    union select 2,5 from dual
)
select distinct feature_details_id, NULL feature_id from data
where feature_details_id not in (
      select feature_details_id from data
      where  feature_id = 5)
;

What the above query will do is - 上面的查询将要做的是-

  1. Select the feature_details_id where feature = 5 选择feature_details_id,其中feature = 5
  2. Remove all those feature_details_id and select whatever is remaining 删除所有这些feature_details_id并选择剩余的内容

With a NOT IN should be ok 用NOT IN应该可以

SELECT DISTINCT FEATURE_DETAILS_ID, NULL AS FEATURE_ID 
    FROM FEATURE_DETAILS 
    WHERE FEATURE_DETAILS_ID NOT IN (
        SELECT FEATURE_DETAILS_ID FROM FEATURE_DETAILS WHERE FEATURE_ID = 5
    );

select feature_deatails_id, null as feature_id from feature_details where feature_id != 5 选择feature_deatails_id,从feature_details中将null作为feature_id,其中feature_id!= 5

This should solve your purpose. 这应该可以解决您的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM