Fix pagination when a "having" clause is added to the query

When using a "having" clause on a query builder, paginating is impossible because the column you're trying to check against won't be part of the select clause. This is crudely fixed by checking if the query has a having clause and then adding the necessary columns as selects.

This honestly feels like somewhat of a hack to me, but all tests pass. The array_diff is there to prevent `select *, count(*)` but there may be a better way.

Related: #3105, #3416, #4532