0

I have table with objects

uuid name
98665e5a-e1cb-4bf7-93fa-a8d13983f358 object

and another table with information about enabled this object and another object together

uuid_from_table_upper enabled uuid_another_object
98665e5a-e1cb-4bf7-93fa-a8d13983f358 true ad5e670a-adac-485b-879e-9448d68a393c
98665e5a-e1cb-4bf7-93fa-a8d13983f358 false a55b021b-ae4b-4373-91ae-87b9f7134862

i need a sql query, return object from first table object, who doesn`t have true in second table

SELECT ft.* 
FROM first_table ft 
  JOIN second_table st on ft.uuid = st.uuid_from_table_upper
WHERE
  not st.enabled;

i guess needed group by or recursive with

3
  • What would be the desired result? What is the meaning of the UUID columns in the second table? Commented Aug 28 at 7:50
  • disired result is null, cause uuid from first table have a true in relations table, UUID columns in the second table is relations
    – smdsa1337
    Commented Aug 28 at 9:02
  • That is not explanation enough for me to understand. Perhaps somebody else can. Commented Aug 28 at 9:05

3 Answers 3

1
SELECT *
FROM first_table 
WHERE NOT EXIST (
    SELECT NULL
    FROM second_table 
    WHERE first_table.uuid = second_table.uuid_from_table_upper
      AND second_table.enabled
    );
0

If you want to do this with joins, you might have to cater for two cases:

  1. table2 contains a row with enabled = false, or
  2. table2 does not contain a matching row at all.

This should cater for both cases:

select t1.*, t2.* 
from first_table t1 
inner join second_table t2 
      on t1.uuid = t2.uuid_from_table_upper
where t2.enabled = false 
UNION ALL 
select t1.*, t2.* 
from first_table t1 
left join second_table t2 
     on t1.uuid = t2.uuid_from_table_upper
where t2.uuid_from_table_upper IS NULL 
ORDER BY ... 
;
0
SELECT first_table.*
FROM first_table
LEFT JOIN second_table on first_table.uuid = second_table.uuid_from_table_upper and not(second_table.enabled)
WHERE second_table.uuid_from_table_upper is null;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.