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 6613173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:11:12+00:00 2026-05-25T20:11:12+00:00

I am looking for something more efficient and/or easier to read than the query

  • 0

I am looking for something more efficient and/or easier to read than the query that follows. The best way to explain the question is to provide a sample.

Assume the following table structure in MySQL that represents, say, a localization context for various strings in the application I am creating.

create table LOCALIZATION_TABLE (
   SET_NAME    varchar(36) not null,
   LOCALE      varchar(8)  not null default '_',
   ENTRY_KEY   varhcar(36) not null,
   ENTRY_VALUE text            null
);

alter table LOCALIZATION_TABLE
  add constraint UQ_ENTRY
  unique (SET_NAME, LOCALE, ENTRY_KEY);

Assume the following values are entered in the table:

insert into LOCALIZATION_TABLE (SET_NAME, LOCALE, ENTRY_KEY, ENTRY_VALUE)
values
  ('STD_TEXT', '_',  'HELLO', 'Hello!'),
  ('STD_TEXT', '_',  'GOODBYE', 'Goodbye.'),
  ('STD_TEXT', 'ge', 'GOODBYE', 'Lebewohl')
;

I want to select all the available entries for German (“ge”), and if not available use the English text (“_”) by default. The query I am currently using is as follows:

select * from LOCALIZATION_TABLE where SET_NAME = 'STD_TEXT' and LOCALE = 'ge'
union
select * from LOCALIZATION_TABLE where SET_NAME = 'STD_TEXT' and LOCALE = '_'
  and ENTRY_KEY not in (
    select ENTRY_KEY from LOCALIZATION_TABLE where BUNDLE = 'STD_TEXT' and LOCALE = 'ge'
  )

I really do not like the look of this query and I am certain there must be something more concise that can be utilized instead. Any help or clues to the right direction would be appreciated. While this works it just does not seem proper.

  • 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-05-25T20:11:13+00:00Added an answer on May 25, 2026 at 8:11 pm

    You can provide a custom ordering, then take the first row, like this:

    select * from (
        select *
        from LOCALIZATION_TABLE
        where SET_NAME = 'STD_TEXT'
        order by field(LOCALE, 'ge', '_') -- this provides the custom ordering
    ) x
    group by ENTRY_KEY; -- this captures the first row for each ENTRY_KEY
    

    Explanation:

    The inner select’s order by field(LOCALE, 'ge', '_') gets you the rows in the order you define – in this case German first if it exists, then English (you could add more languages to the list).

    The “trick” here is using mysql’s “non-standard” GROUP BY behaviour when not listing the non-group columns (most servers treat this as a syntax error) – it simply returns the first row found for every group. The outer select uses group by without an aggregate to get the first row for each named group by.

    Output of this query using your data:

    +----------+--------+-----------+-------------+
    | SET_NAME | LOCALE | ENTRY_KEY | ENTRY_VALUE |
    +----------+--------+-----------+-------------+
    | STD_TEXT | ge     | GOODBYE   | Lebewohl    |
    | STD_TEXT | _      | HELLO     | Hello!      |
    +----------+--------+-----------+-------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more efficient way to clamp real numbers than using if statements
I realize you can script Microsoft Office apps, but I'm looking for something more
I'm looking for something that will show me the size of each folder within
I'm looking for something that will monitor Windows directories for size and file count
I am looking for some simple and efficient parameter container that would act like
I am looking for a robust, efficient data compression algorithm that I could use
Couldn't think of a more appropriate question title, but I'm looking for some advice
   I'm currently looking for a more efficient method, via VBA, to get information transferred
Can someone recommend a simple c# code generator. I just looking something with methods
Looking for something like: $(input:radio:checked).previous(name, original_name).attr(name,new_name); I tried a few different seen around here

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.