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 use update with select in mysql? #158

Open
oh0123 opened this issue Jul 9, 2024 · 2 comments
Open

how to use update with select in mysql? #158

oh0123 opened this issue Jul 9, 2024 · 2 comments

Comments

@oh0123
Copy link

oh0123 commented Jul 9, 2024

here is code

UPDATE T1
[INNER JOIN | LEFT JOIN] T2 ON T1.C1 = T2. C1
SET T1.C2 = T2.C2, 
    T2.C3 = expr
WHERE condition
@huandu
Copy link
Owner

huandu commented Jul 10, 2024

There is no built-in support for JOIN in UpdateBuilder. Instead, you can use SQL() method to build this SQL.

ub := sqlbuilder.Update("T1").SQL("INNER JOIN T2 ON T1.C1 = T2. C1")
ub.Set(
    "T1.C2 = T2.C2",
    ub.Assign("T2.C3", 123),
)
ub.Where(ub.In("T1.C4", "foo", "bar"))

Here is a live demo for you. https://go.dev/play/p/xfwm9P-55sX

@oh0123
Copy link
Author

oh0123 commented Jul 11, 2024

@huandu that‘s really nice, I will try it. thanks!

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

2 participants