Skip to content
Advertisement

Select adjacent rows in mysql – but when using alphabetical order of specific column, not numeric order of ID

I have a wordpress install with custom post types where I would like to find the ‘previous’ and ‘next’ custom post type object via mysql.

I have read many questions/answers on SO regarding how to achieve this when using the ID. The issue is that a lot of these objects were input at different times, so their numeric ‘id’ order does not necessarily correspond with their string post_title order.

eg:

The SQL query:

Gives me the results. If I know the ID, for example b, 1 from above, how could I find that, when ordered by post_title, that the adjacent posts are 7 and 3?

Advertisement

Answer

You can do this with union all:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement