Home > database >  PostgreSQL : Display the id which do not have a specific attribute value
PostgreSQL : Display the id which do not have a specific attribute value

Time:07-12

Structure

ID Attribute_id Value
id_1 attribute_1 name_1
id_1 attribute_1 name_2
id_1 attribute_1 name_3
id_1 attribute_2 xxx
id_1 attribute_3 yyy
id_2 attribute_2 aaa
id_2 attribute_3 bbb
id_3 attribute_1 name_1
id_3 attribute_2 xxx

Result expected

ID
id_2

Need to display ids not having name (not associated with attribute_1) How to achieve this?

CodePudding user response:

One option is to use EXCEPT operator:

select id from your_table 
except
select id from your_table where attribute_id = 'attribute_1';
  • Related