Hello everyone,
I'm having a problem with a particular MySQL query.
I have table1
, and table2
, table2
is joined onto table1
.
Now the problem is that I am joining table2
to table1
with a condition that looks like:
SELECT
table1.*, table2.*
JOIN table2 ON ( table2.table1_id = table1.id
AND ( table2.lang = 'fr'
OR table2.lang = 'eu'
OR table2.lang = 'default') )
I need it to return only 1 row from table2
, even though there might exists many table2
rows for the correct table1.id
, with many different locales.
I am looking for a way to join only ONE row with a priority of the locales, first check for one where lang = something
, then if that doesn't manage to join/return anything, then where lang = somethingelse
, and lastly lang = default
.
FR, EU can be different for many users, and rows in the database might exist for many different locales.. I need to select the most suitable ones with the correct fallback priority.
I tried doing the query above with a GROUP BY table2.table1_id
, and it seemed to work, but I realised that if the best matching (first OR) was entered later in the table (higher primary ID) it would return 2nd or default priority as the grouped by row..
Any tips?
Thank you!