Count (*) (asterisk) on Querydsl / MySQL?
The original MySQL functional query that lists all and only vendors that have all of the listed tags:
SELECT * FROM provider
INNER JOIN provider_tag
ON provider_tag.provider_id = provider.id AND provider_tag.tag_id in (1, 2)
GROUP BY (provider.id)
HAVING COUNT(*) = 2
Translating to MySQLQuery to Querydsl is easy ...
MySQLQuery query = new MySQLQuery(conn, dialect);
List<Integer> tagIds = ...;
query.from(provider)
.innerJoin(provider_tag)
.on(providerTag.providerId.eq(provider.id), providerTag.tagId.in(tagIds))
.groupBy(provider.id)
.having(???);
... except for clause c having
.
How to add COUNT(*)
to the request?
EDIT after Timo 1's fix suggestion :
So the request looks like this:
SearchResults<Tuple> result = query.from(provider)
.innerJoin(providerTag)
.on(providerTag.providerId.eq(provider.id), providerTag.tagId.in(tagIds))
.groupBy(provider.id)
.having(Wildcard.count.eq((long) tagIds.size()))
.listResults(
provider.id,
provider.name);
However, this throws an SQLException Illegal operation on empty result set
if the result set is empty.
My other queries that return an empty result set don't throw exceptions, so I guess I wouldn't need to catch the exception, but is there a problem that needs to be fixed?
The generated MySQL works fine (returns 0 rows) so there is no problem there.
EDIT 2 :
The problem was groupBy()
. This seems to work if you apply the correction shown in the issue .
source to share