Java – count (*) (asterisk) on querydsl / MySQL?
The initial function is MySQL query, which lists all providers of all 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
Converting to mysqlquery in querydsl is very simple
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 conditional
How to add count (*) to a query?
After Timo's first amendment proposal, edit:
So the query 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, if the result set is empty, it will cause sqlexception illegal operation on the empty result set
My other queries that return empty result sets will not cause exceptions, so I don't think I should catch exceptions, but there is a problem that needs to be fixed?
The generated MySQL works normally (returns 0 rows), so the problem does not exist
Edit 2:
The problem is in groupby() This appears to be valid if the correction shown in issue is applied
Solution
The querydsl equivalent of count (*) is wildcard count.