How to use “WHERE” and also “WHERE NOT EXISTS” in the same query and table?
-
06-01-2021 - |
Question
I am trying to add both WHERE
and WHERE NOT EXISTS
in same query. Below mentioned query1 is working fine, but it returns all the t1 data which does not belong to company1. So I tried query2, but no luck. How can I get the desired result?
1.
select * from t1 where not exists
(select * from t2 inner join t3 on(t3._id = t2.department_id)where t2.category_id = t1._id AND t3._id = 27 AND t3.company_id = 1)
ORDER BY "ctsName"
2.
select * from t1 where company_id = 1 AND where not exists
(select * from t2 inner join t3 on(t3._id = t2.department_id)where t2.category_id = t1._id AND t3._id = 27 AND t3.company_id = 1)
ORDER BY "ctsName"
Solution
WHERE
is a separator, not an operation , the bit after WHERE
describes the rows you want to see in the result.
To apply two conditons use AND
or OR
to combine them.
select * from t1
where not exists (select * from t2 inner join t3 on(t3._id = t2.department_id)
AND t2.category_id = t1._id AND t3._id = 27 AND t3.company_id = 1)
ORDER BY "ctsName"
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange