@@ -12,6 +12,7 @@ type UpdateBuilder struct {
12
12
prefixes []SQLizer
13
13
table string
14
14
setClauses []setClause
15
+ fromParts []SQLizer
15
16
whereParts []SQLizer
16
17
orderBys []string
17
18
returning []SQLizer
@@ -70,6 +71,14 @@ func (b UpdateBuilder) SQL() (sqlStr string, args []any, err error) {
70
71
}
71
72
sql .WriteString (strings .Join (setSQLs , ", " ))
72
73
74
+ if len (b .fromParts ) > 0 {
75
+ sql .WriteString (" FROM " )
76
+ args , err = appendSQL (b .fromParts , sql , ", " , args )
77
+ if err != nil {
78
+ return
79
+ }
80
+ }
81
+
73
82
if len (b .whereParts ) > 0 {
74
83
sql .WriteString (" WHERE " )
75
84
args , err = appendSQL (b .whereParts , sql , " AND " , args )
@@ -153,6 +162,26 @@ func (b UpdateBuilder) SetMap(clauses map[string]any) UpdateBuilder {
153
162
return b
154
163
}
155
164
165
+ // From adds FROM expressions to the query.
166
+ //
167
+ // A table expression allowing columns from other tables to appear in the WHERE condition and update expressions.
168
+ // This uses the same syntax as the FROM clause of a SELECT statement.
169
+ // Do not repeat the target table unless you intend a self-join (in which case, you must use an alias).
170
+ func (b UpdateBuilder ) From (items ... string ) UpdateBuilder {
171
+ parts := make ([]SQLizer , 0 , len (items ))
172
+ for _ , str := range items {
173
+ parts = append (parts , newPart (str ))
174
+ }
175
+ b .fromParts = append (b .fromParts , parts ... )
176
+ return b
177
+ }
178
+
179
+ // FromSelect adds FROM expressions to the query similar to From, but takes a Select statement.
180
+ func (b UpdateBuilder ) FromSelect (from SelectBuilder , alias string ) UpdateBuilder {
181
+ b .fromParts = append (b .fromParts , Alias {Expr : from , As : alias })
182
+ return b
183
+ }
184
+
156
185
// Where adds WHERE expressions to the query.
157
186
//
158
187
// See SelectBuilder.Where for more information.
0 commit comments