1
- # SubQuery Support
2
- The library currently supports subQueries in the following areas:
1
+ # Subquery Support
2
+ The library currently supports subqueries in the following areas:
3
3
4
4
1 . In certain where conditions
5
5
1 . In certain insert statements
6
+ 1 . In update statements
6
7
1 . In the "from" clause of a select statement
7
8
8
- ## SubQueries in Where Conditions
9
- The library support subQueries in the following where conditions:
9
+ ## Subqueries in Where Conditions
10
+ The library support subqueries in the following where conditions:
10
11
11
12
- isEqualTo
12
13
- isNotEqualTo
@@ -33,7 +34,7 @@ SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, bra
33
34
```
34
35
35
36
### Kotlin Support
36
- The library includes Kotlin versions of the where conditions that allow use of the Kotlin subQuery builder. The Kotlin
37
+ The library includes Kotlin versions of the where conditions that allow use of the Kotlin subquery builder. The Kotlin
37
38
where conditions are in the ` org.mybatis.dynamic.sql.util.kotlin ` package. An example is as follows:
38
39
39
40
``` kotlin
@@ -47,7 +48,7 @@ val selectStatement = select(id, firstName, lastName, birthDate, employed, occup
47
48
}
48
49
```
49
50
50
- ## SubQueries in Insert Statements
51
+ ## Subqueries in Insert Statements
51
52
The library supports an INSERT statement that retrieves values from a SELECT statement. For example:
52
53
53
54
``` java
@@ -64,7 +65,7 @@ InsertSelectStatementProvider insertSelectStatement = insertInto(animalDataCopy)
64
65
65
66
### Kotlin Support
66
67
67
- The library includes a Kotlin builder for subQueries in insert statements that integrates with the select DSL. You
68
+ The library includes a Kotlin builder for subqueries in insert statements that integrates with the select DSL. You
68
69
can write inserts like this:
69
70
70
71
``` kotlin
@@ -77,9 +78,42 @@ val insertStatement = insertSelect(Person) {
77
78
}
78
79
```
79
80
80
- ## SubQueries in a From Clause
81
+ ## Subqueries in Update Statements
82
+ The library supports setting update values based on the results of a subquery. For example:
81
83
82
- The library supports subQueries in from clauses and the syntax is a natural extension of the
84
+ ``` java
85
+ UpdateStatementProvider updateStatement = update(animalData)
86
+ .set(brainWeight). equalTo(
87
+ select(avg(brainWeight))
88
+ .from(animalData)
89
+ .where(brainWeight, isGreaterThan(22.0 ))
90
+ )
91
+ .where(brainWeight, isLessThan(1.0 ))
92
+ .build()
93
+ .render(RenderingStrategies . MYBATIS3 );
94
+ ```
95
+
96
+ ### Kotlin Support
97
+ The library includes a Kotlin builder for subqueries in update statements that integrates
98
+ with the select DSL. You can write subqueries like this:
99
+
100
+ ``` kotlin
101
+ val updateStatement = update(Person ) {
102
+ set(addressId).equalToQueryResult {
103
+ select(add(max(addressId), constant<Int >(" 1" ))) {
104
+ from(Person )
105
+ }
106
+ }
107
+ where(id, isEqualTo(3 ))
108
+ }
109
+ ```
110
+
111
+ Note the Kotlin method name is ` set(xxx).equalToQueryResult(...) ` - this is to avoid a collison with
112
+ other methods in the update DSL.
113
+
114
+ ## Subqueries in a From Clause
115
+
116
+ The library supports subqueries in from clauses and the syntax is a natural extension of the
83
117
select DSL. An example is as follows:
84
118
85
119
``` java
@@ -102,7 +136,7 @@ SelectStatementProvider selectStatement =
102
136
Notice the use of a ` DerivedColumn ` to easily specify a function like ` rownum() ` that can be
103
137
used both in the select list and in a where condition.
104
138
105
- ### Table Qualifiers with SubQueries
139
+ ### Table Qualifiers with Subqueries
106
140
107
141
The library attempts to automatically calculate table qualifiers. If a table qualifier is specified,
108
142
the library will automatically render the table qualifier on all columns associated with the
@@ -128,12 +162,12 @@ Notice that the table qualifier `ad` is automatically applied to columns in the
128
162
In the case of join queries the table qualifier specified, or if not specified the table name
129
163
itself, will be used as the table qualifier.
130
164
131
- With subQueries , it is important to understand the limits of automatic table qualifiers. The rules are
165
+ With subqueries , it is important to understand the limits of automatic table qualifiers. The rules are
132
166
as follows:
133
167
134
- 1 . The scope of automatic table qualifiers is limited to a single select statement. For subQueries , the outer
135
- query has a different scope than the subQuery .
136
- 1 . A qualifier can be applied to a subQuery as a whole, but that qualifier is not automatically applied to
168
+ 1 . The scope of automatic table qualifiers is limited to a single select statement. For subqueries , the outer
169
+ query has a different scope than the subquery .
170
+ 1 . A qualifier can be applied to a subquery as a whole, but that qualifier is not automatically applied to
137
171
any column
138
172
139
173
As an example, consider the following query:
@@ -168,10 +202,10 @@ where rownum() < #{parameters.p2}
168
202
and animal_name like # {parameters.p3}
169
203
```
170
204
171
- Notice that the qualifier ` a ` is automatically applied to columns in the subQuery and that the
205
+ Notice that the qualifier ` a ` is automatically applied to columns in the subquery and that the
172
206
qualifier ` b ` is not applied anywhere.
173
207
174
- If your query requires the subQuery qualifier to be applied to columns in the outer select list,
208
+ If your query requires the subquery qualifier to be applied to columns in the outer select list,
175
209
you can manually apply the qualifier to columns as follows:
176
210
177
211
``` java
@@ -207,7 +241,7 @@ where rownum() < #{parameters.p2}
207
241
208
242
### Kotlin Support
209
243
210
- The library includes a Kotlin builder for subQueries that integrates with the select DSL. You
244
+ The library includes a Kotlin builder for subqueries that integrates with the select DSL. You
211
245
can write queries like this:
212
246
213
247
``` kotlin
@@ -225,7 +259,7 @@ val selectStatement =
225
259
}
226
260
```
227
261
228
- The same rules about table qualifiers apply as stated above. In Kotlin, a subQuery qualifier
262
+ The same rules about table qualifiers apply as stated above. In Kotlin, a subquery qualifier
229
263
can be added with the overloaded "+" operator as shown below:
230
264
231
265
``` kotlin
@@ -245,4 +279,4 @@ val selectStatement =
245
279
```
246
280
247
281
In this case the ` a ` qualifier is used in the context of the inner select statement and
248
- the ` b ` qualifier is applied to the subQuery as a whole.
282
+ the ` b ` qualifier is applied to the subquery as a whole.
0 commit comments