Skip to content
Advertisement

Fetch page data of specific OR other language

I have a table with webpage data that looks somewhat like this:

I want to fetch a single language version of each page in a prioritized order. Let’s say I’d like to fetch all pages of fr language, but if some pages lack that language version, then get the en version instead, but again if there’s no en, try pl, and so on; so in this case the result would look like this:

The language count is not predifined, however each page is bound to have at least one language version.

It’s possible to do with a whoopload of queries, or by fetching all the data and then process it programatically by PHP, but I was wondering if there is an elegant SQL (SQLite) solution that would solve this in one query?

Advertisement

Answer

With conditional aggregation:

See the demo.

Or if your version of SQLite allows the use of window functions:

See the demo.

Results:

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