Here are my tables:
mysql> SELECT * FROM locale;
+-----------+---------+
| locale_id | value |
+-----------+---------+
| 1 | English |
| 2 | Deutch |
| 3 | French |
+-----------+---------+
mysql> SELECT * FROM terms;
+----------+---------------+
| terms_id | value |
+----------+---------------+
| 1 | lname |
| 2 | lpass |
| 3 | welcome_blurb |
+----------+---------------+
mysql> SELECT * FROM rosetta;
+------------+-----------+----------+-------------------------------------------------+
| rosetta_id | locale_id | terms_id | value |
+------------+-----------+----------+-------------------------------------------------+
| 1 | 1 | 1 | Username: |
| 2 | 2 | 1 | Benutzername: |
| 3 | 1 | 2 | Password: |
| 4 | 2 | 2 | Passwort: |
| 5 | 1 | 3 | Welcome to appland! Please log in below. |
| 6 | 2 | 3 | Welcome to appland Bitte melden Sie sich unten. |
+------------+-----------+----------+-------------------------------------------------+
What I want is a query that contains the locale_id, and it returns all the terms.value and rosetta.value for that locale, if it exists. If it hasn't been defined in rosetta table yet, then have empty values. I'd like it to 'just work' via clever JOINs, and not have IF logic in there if possible.
Desired output:
If I give the query locale_id=1:
+---------------+------------------------------------------+
| terms.value | rosetta.value |
+---------------+------------------------------------------+
| lname | Username: |
| lpass | Password: |
| welcome_blurb | Welcome to appland! Please log in below. |
+---------------+------------------------------------------+
This one I can get working just fine with:
SELECT terms.value,rosetta.value as definition
FROM terms
LEFT JOIN rosetta ON terms.terms_id=rosetta.terms_id
WHERE rosetta.locale_id=1 OR rosetta.locale_id IS NULL;
If I give the query locale_id=3, what I want back is:
+---------------+---------------+
| terms.value | rosetta.value |
+---------------+---------------+
| lname | |
| lpass | |
| welcome_blurb | |
+---------------+---------------+
However, I just get an empty result.
I was thinking something like this could work:
SELECT terms.value,
IF(rosetta.locale_id=3,rosetta.value,'') as definition
FROM terms
LEFT JOIN rosetta ON terms.terms_id=rosetta.terms_id
WHERE rosetta.locale_id=3 OR rosetta.locale_id IS NULL;
However it doesn't. Any suggestions? Really banging my head against a wall with this one.
Link to the SQL Fiddle with the content.