Skip to content

Multi Select Queries #591

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

Merged
merged 5 commits into from
Feb 23, 2023
Merged

Conversation

jeffgbutler
Copy link
Member

A multi-select query is a special case of a union select statement. The difference is that it allows "order by" and
paging clauses to be applied to the nested queries.

Example Java code:

SelectStatementProvider selectStatement = multiSelect(
        select(id, firstName, lastName, birthDate, employed, occupation, addressId)
                .from(person)
                .where(id, isLessThanOrEqualTo(2))
                .orderBy(id)
                .limit(1)
).union(
        select(id, firstName, lastName, birthDate, employed, occupation, addressId)
                .from(person)
                .where(id, isGreaterThanOrEqualTo(4))
                .orderBy(id.descending())
                .limit(1)
)
.orderBy(id)
.limit(2)
.build()
.render(RenderingStrategies.MYBATIS3);

Example Kotlin code:

val selectStatement = multiSelect {
    selectDistinct(id, firstName, lastName, birthDate, employed, occupation, addressId) {
        from(person, "p1")
        where { id isLessThanOrEqualTo 2 }
        orderBy(id)
        limit(1)
    }
    union {
        select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
            from(person, "p2")
            where { id isGreaterThanOrEqualTo 4 }
            orderBy(id.descending())
            limit(1)
        }
    }
    orderBy(id)
    limit(2)
}

Resolves #586

@jeffgbutler jeffgbutler added this to the 1.5.0 milestone Feb 23, 2023
@coveralls
Copy link

coveralls commented Feb 23, 2023

Coverage Status

Coverage: 100.0%. Remained the same when pulling 9b9c04a on jeffgbutler:multi-select-union into ba8a43a on mybatis:master.

@jeffgbutler jeffgbutler merged commit 4d1957d into mybatis:master Feb 23, 2023
@jeffgbutler jeffgbutler deleted the multi-select-union branch February 23, 2023 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to do union/unionAll with order for different queries?
2 participants