Reputation: 125
I have a table Users looks like this:
ID COMPANY_ID FIRST_NAME
1 1 John
2 1 Mary
3 2 Ivan
4 1 Arnold
5 3 Sam
I need SQL query that may check that pairs exists for user_id, company_id
of one company and return [[id, true], [id, false]
.
It mean I want ask if users [1, 4, 7, 17]
of company 2
exists and receive [[1, true], [4, true], [7, false], [17, true]]
.
It would be a great if you may suggest how do this with sqlalchemy Core.
Upvotes: 2
Views: 1574
Reputation: 1270503
In SQL, you would use left join
. Assuming Users
has not duplicates, you would do something like:
select t.id, (case when u.id is null then 0 else 1 end) as flag
from (select 1 as id union all
select 4 union all
select 7 union all
select 17
) t left join
users u
on u.id = t.id and u.company = 2;
Upvotes: 1