I am implementing a seek method for pagination and am wondering about how to best query on a text column with DESC. The queries for this seek approach use a less than or greater than depending on if you are sorting ASC or DESC. This works great for integers and dates but I am wondering how best to do it with text columns, specifically for the first page.
For example, for the first page when sorting by name it would be
SELECT *
FROM users
WHERE first_name > ''
ORDER BY first_name ASC
LIMIT 5;
Then the next page would be
SELECT *
FROM users
WHERE first_name > 'Caal'
ORDER BY first_name ASC
LIMIT 5;
This works great. I am unsure about DESC order though. This seems to work but I am unsure if it is 'correct'.
SELECT *
FROM users
WHERE last_name < 'ZZZ'
ORDER BY last_name DESC
LIMIT 5;
Second page
SELECT *
FROM users
WHERE last_name < 'Smith'
ORDER BY last_name DESC
LIMIT 5;
P.S. I am using the jooq support for the seek method and prefer to not have to hack around the native support, so ideally there is a proper parameter to put in the 'ZZZ' place above. i.e. there WHERE part of the clause is mandatory.
ZZZ
is the last possible name is based on a limited view on names, such as: 1) They always start with an upper case, 2) Names starting withZ
don't have a lower case letter at their second position. 3) There do not exist any unicode names, e.g. starting withŽ
, 4) etc... Also, you're assuming that there are no two people with the same last name.Field
toseekBefore()
: jooq.org/javadoc/latest/org/jooq/…. I suspect a trick would be to use the very last character of the collation (which is very unlikely to be used inlast_name
)seek()
clause.