Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do "SELECT EXISTS(SELECT 1 FROM ...)"? #619

Open
Bobsans opened this issue Oct 6, 2022 · 3 comments
Open

How to do "SELECT EXISTS(SELECT 1 FROM ...)"? #619

Bobsans opened this issue Oct 6, 2022 · 3 comments

Comments

@Bobsans
Copy link

Bobsans commented Oct 6, 2022

Maybe need to add method query.AsExists() or any other for do this?

@nielslucas
Copy link

nielslucas commented Nov 28, 2022

image

Can some of these help you?
Or you can always use a raw querie

@Bobsans
Copy link
Author

Bobsans commented Nov 29, 2022

These are methods for adding conditions.
For example .HavingExists(...) will generate SELECT ... FROM ... HAVING EXISTS(...).
And .WhereExists(...) will generate SELECT ... WHERE EXISTS(...).
The Query class has a .AsCount() method that generates SELECT COUNT(*) FROM ....
My question is how to generate exactly SELECT EXISTS(...)
Maybe add a method like .AsExists() that will generate a query that will have SELECT EXISTS(...) in the view?

@fairking
Copy link

fairking commented Mar 14, 2023

I am looking for something similar, but in my case it is:

select
    case when exists (select * from sessions where user_id = u.id) then 1 else 0 end is_current
from users u

So I am literally would like to include any query into the raw query, like:

query.SelectRawFormat("case when exists {0} then 1 else 0 end is_current", sub_query)

So the approach could be more flexible, eg:

query.SelectRawFormat("case when exists {0} then ? when exists {1} then ? else 0 end is_current", 
    formats: new object[] { sub_query1, sub_query2 },
    bindings: new object[] { 1, 2 })

as a current workaround is:

select
    case when s.user_id is not null then 1 else 0 end is_current
from users u
    left join (select user_id from sessions group by user_id) s on s.user_id = u.id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants