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

SQL Exists operator #100

Closed
disog opened this issue Mar 28, 2023 · 2 comments
Closed

SQL Exists operator #100

disog opened this issue Mar 28, 2023 · 2 comments

Comments

@disog
Copy link

disog commented Mar 28, 2023

I didn't find a builder.Exists() method in the codebase. Is there a way to accomplish this?

@doyonghoon
Copy link

Hey, I hope the following suggestion may help.

An ultimate function to generate whatever the query you'd like to have is using SQL(raw_sql string). Examples are found in the following link: https://pkg.go.dev/github.com/huandu/go-sqlbuilder#SelectBuilder.SQL

@huandu
Copy link
Owner

huandu commented Mar 28, 2023

I plan to support EXISTS, NOT EXISTS, ANY, SOME and ALL in builders which have WHERE clause. I'll finish it in this weekend.

Before that, you can simply write code like following.

sb := sqlbuilder.Select("col").From("table1")
subquery := sqlbuilder.Select("1").From("table2").Where("status = 1")

// Use sb.Var() and Buildf to build EXISTS clause.
sb.Where(
    sb.Var(sqlbuilder.Buildf("EXISTS(%v)", subquery)),
)

println(sb.String())

// Output:
// SELECT col FROM table1 WHERE EXISTS(SELECT 1 FROM table2 WHERE status = 1)

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

No branches or pull requests

3 participants