I’m trying to return values from a join where the where clause might not have a matches.
Here’s my database schema
strings
-------
id: INT
name: VARCHAR
value: VARCHAR
fileId: INT FOREIGN KEY files(id)
languages
---------
id: INT
code: CHAR
name: VARCHAR
translations
------------
id: INT
string_id: INT, FOREIGN KEY strings(id)
language_id: INT, FOREIGN KEY languages(id)
translation: VARCHAR
I’m trying to select all the strings
, and all the translations
in a given language. The translations may or may not exist for a given language, but I want to return the strings
any way.
I’m using a query similar to:
SELECT s.id, s.name, s.value, t.translation
FROM strings s LEFT OUTER JOIN translations t ON s.id = t.string_id
WHERE s.file_id = $1 AND t.language_id = $2
I want to return strings regardless of whether matches are found in the translations table. If translations don’t exist for a given language, that field would of course be null. I think the problem is with the WHERE
clause having the t.language_id = ...
, since language_id
doesn't exist in this particular case. But not sure the best way to fix this.
Database Postgresql