Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:16:10+00:00 2026-06-17T23:16:10+00:00

I have two tables: posts and post_translations . A post can be translated into

  • 0

I have two tables: posts and post_translations. A post can be translated into many languages, and its translations are stored in the post_translations table. Not all posts are available in all languages.

posts:

+----+------+
| id | hits |
+----+------+
|  1 |   12 |
+----+------+
|  2 |   34 |
+-----------+

post_translations:

+----+---------+--------+---------------+------+
| id | post_id | locale | title         | body |
+----+---------+--------+---------------+------+
|  1 |       1 |     en | Hello, world! | Hey. |
+----+---------+------------------------+------+
|  2 |       1 |     es | ¡Hola, mundo! | Olé. |
+----+---------+--------+---------------+------+
|  3 |       2 |     en |  How are you? | Meh. |
+----+---------+--------+---------------+------+

I’d like to SELECT all posts, ordered by Spanish title—but, since not all posts are available in Spanish, I’d also like to fall back to English title if necessary. That is, ORDER BY title_with_fallbacks where title_with_fallbacks = [spanish title] || [english title].

I suppose I could use a dependent subquery:

SELECT * FROM posts ORDER BY (SELECT name FROM post_translations
                              WHERE post_id = posts.id
                              ORDER BY FIELD(locale, 'es', 'en')
                              LIMIT 1)

But that could get nasty fast if there are thousands of results. Any clever ideas of how to get the same result by joining the two tables or something along those lines?

(For reference, I’m using the Rails plugin globalize3, but I haven’t been able to find any built-in mechanism to get this job done.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:16:11+00:00Added an answer on June 17, 2026 at 11:16 pm

    This query accomplishes the conditional ordering with two left joins… One to the Spanish translation, and another to the English translation…

    The ORDER BY then uses the IFNULL function to order by the Spanish title, and if the Spanish title is NULL, order by the English title instead.

    SELECT p.id, p.hits, IFNULL(es_pt.title, en_pt.title) AS locale_title
    FROM posts p
    LEFT JOIN post_translations es_pt
        ON p.id = pt.post_id AND es_pt.locale = 'es'
    LEFT JOIN post_translations en_pt
        ON p.ID = pt.post_id AND en_pt.locale = 'en'
    ORDER BY IFNULL(es_pt.title, en_pt.title)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables POSTS and COMMENTS. One post can have many comments and
I have two tables Posts and comments Post Table Post_id Post_content Comments table comment_id
I have two tables: posts and custom_fields. A post can have multiple custom_fields. Posts
I have two tables. Table 1. ======== id post -------- Table 2. ======== id
I have two tables posts and comments . Table comments have post_id attribute. I
Okay so I have two tables, a category table and a posts table. I
I have two tables: posts - holds post information listen - holds information on
I have two tables Post (id, ..., creationdate) and Post_Has_Post(parent_id, child_id) . Some Posts
Suppose we have two tables. Post and Comment. Post has many Comments. Pretend they
I have two tables post Table post_id post_name post_summary date post_reply Table parent_id post_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.