我需要一些帮助,因为我无法弄清楚。
我有两张桌子
餐桌食谱
列 :
ID, Recipe_name, Instructions
表格翻译
列 :
ID, RecipeID, Translation, Country_ID
我要实现的目标是,当用户查找食物食谱并且希望以自己的语言(法国或其他语言)获取食物时,MYSQL将向左(或向右)加入翻译文本。并且,如果没有找到翻译的文本,则应返回NULL
我尝试了这个:
SELECT Recipes.* , Translations.Translation FROM Recipes
INNER JOIN Translations.Translation
ON Recipes.ID = Translations.RecipeID
WHERE Recipes.ID = 999 AND COALESCE(Translations.Country_ID, '') != '13'
Limit 1
Recipes.ID = 999 <-
是存储在dbase中的配方Translations.Translation = 13 <-
国家ID,在这种情况下为法国上面的语句返回时,看起来好像是随机翻译的文本,我只需要13(法国),如果没有翻译的文本,我希望它返回NULL。
提前致谢。
编辑-使用表格:
**Table Recipes
|ID | Recipe_name | Instructions
----+--------------+-------------
| 1 | Tasty Dish | 1 spoon of sugar
| 2 | Chicken | 1 chicken
** Table Translations
|ID | Recipe_ID | Translation | Country_ID
----+------------+---------------------+-----------
| 1 | 1 | 1 cuillère de sucre | 13 (france)
| 2 | 1 | 1 Lepel Suiker | 11 (dutch)
没有法语翻译的另一种情况:
**Table Recipes
|ID | Recipe_name | Instructions
----+--------------+-------------
| 1 | Tasty Dish | 1 spoon of sugar
| 2 | Chicken | 1 chicken
** Table Translations
|ID | Recipe_ID | Translation | Country_ID
----+------------+---------------------+-----------
| 2 | 1 | 1 Lepel Suiker | 11 (dutch)
MYSQL仍应添加列TRANSLATION,但填充为NULL
最佳答案
您的查询似乎有点困惑。您说要使用外部联接(左或右),但是要使用内部联接。只需使用外部联接,查询就很简单:
select r.* , t.translation
from recipes r
left join translations t on t.recipeid = r.id and t.country_id = 13
where r.id = 999;