I have a table with products and another table with the names of these products (a product can have several names in different languages).
I currently use a query like this to list the names of all products:
SELECT name
FROM name_table
LEFT JOIN product_table ON name_table.product = product_table.product
ORDER BY name_table.product, language != 'en', language != '*'
This would yield a list of all names and the best available name (english if there is one, then an international name, if neither exists any name) would always appear first. I just skip over all entries after the first for each product - not elegant but it works fast enough.
The problem with this is that I cannot sort alphabetically.
However, when I use something like this:
SELECT
product,
(SELECT name
FROM name_table
WHERE name_table.product = product_table.product
ORDER BY language != 'en'
LIMIT 1) AS bestname
FROM product_table
ORDER BY bestname
then the query is extremely slow (at least with MySQL).
This query is fast but it seems to be undefined (therefore quasi-random) what name row is used:
SELECT name
FROM product_table
LEFT JOIN name_table ON name_table.product = product_table.product
ORDER BY name
If I only could tell MySQL what row to use for the join, then all would be fine.
Is there a way to define which row is used in a join if several matching rows exist?
Edit: Products can have names in any languages and not all products have an english name, sorry for the confusion.
Edit2: I have found an equivalent question: MySQL JOIN the most recent row only? There, the SQL-gurus also used sub-selects (my subselect-query is extremely slow so I want to avoid it), so maybe SQL cannot do what I want :-(