Select 行 WHERE 列中的任何行等于 1,否则列等于 2,但不能同时为 2
Select rows WHERE any row in column is equal to 1 or if not, column is equal to 2, but not both
我有一个翻译 table 我的文字如下:
Table:待办事项
day | text_id
-------------
0 | 1
1 | 2
1 | 1
Table:翻译
lang | text_id | text
---------------------
deu | 1 | Laufen
eng | 1 | Running
eng | 2 | Swimming
现在我想用德语 (deu) 查找我的待办事项。我的问题是,我没有 text_id 2 的翻译(例如):德语游泳。
我的默认查询是:
SELECT todos.day, translations.text
INNER Join translations
ON todos.text_id = translations.text_id
WHERE translations.locale = 'deu';
我会得到:
day | text
--------------
0 | Laufen
1 | Laufen
但我想要:
day | text
--------------
0 | Laufen
1 | Swimming
1 | Laufen
我怎样才能得到一些缺失的行?首先我应该得到所有需要的行:
SELECT todos.day, translations.text
INNER Join translations
ON todos.text_id = translations.text_id
WHERE translations.locale = 'deu' or translations.locale = 'eng';
然后删除所有重复的 'eng' 但是 - 怎么做?
抱歉这个糟糕的标题,我不知道如何恰当地描述它...
您需要left join
保留第一个table中的所有记录。然后你需要它两次来获取默认的英文记录:
SELECT td.day, coalesce(tdeu.text, teng.text) as text
FROM todos td left join
translations tdeu
ON td.text_id = tdeu.text_id and tdeu.locale = 'deu' left join
translations teng
ON td.text_id = teng.text_id and teng.locale = 'eng';
我有一个翻译 table 我的文字如下:
Table:待办事项
day | text_id
-------------
0 | 1
1 | 2
1 | 1
Table:翻译
lang | text_id | text
---------------------
deu | 1 | Laufen
eng | 1 | Running
eng | 2 | Swimming
现在我想用德语 (deu) 查找我的待办事项。我的问题是,我没有 text_id 2 的翻译(例如):德语游泳。
我的默认查询是:
SELECT todos.day, translations.text
INNER Join translations
ON todos.text_id = translations.text_id
WHERE translations.locale = 'deu';
我会得到:
day | text
--------------
0 | Laufen
1 | Laufen
但我想要:
day | text
--------------
0 | Laufen
1 | Swimming
1 | Laufen
我怎样才能得到一些缺失的行?首先我应该得到所有需要的行:
SELECT todos.day, translations.text
INNER Join translations
ON todos.text_id = translations.text_id
WHERE translations.locale = 'deu' or translations.locale = 'eng';
然后删除所有重复的 'eng' 但是 - 怎么做?
抱歉这个糟糕的标题,我不知道如何恰当地描述它...
您需要left join
保留第一个table中的所有记录。然后你需要它两次来获取默认的英文记录:
SELECT td.day, coalesce(tdeu.text, teng.text) as text
FROM todos td left join
translations tdeu
ON td.text_id = tdeu.text_id and tdeu.locale = 'deu' left join
translations teng
ON td.text_id = teng.text_id and teng.locale = 'eng';