Mysql select with Where 和 default if where 条件不存在

Mysql select with Where and default if where condition not present

我有表 Product 和 productDetails。在 productDetails 中,我有用多种语言描述产品的行(由 lang 列区分)。并非每个产品都有每种语言的描述。如何制作一个 select 离子,它将 select 指定语言的描述(通过 Where productDescription.lang = 'en'),并且如果指定的语言是 select 默认语言的描述不存在(使用默认语言的描述始终存在)。

我做了这个:

select *
  from product
  left join productDetails on product.id = productDetails.product_id
 where productDetails.language = 'en';

而且我得到了英语的所有详细信息。如果 en 不存在,如何修改它以使用默认语言 selected 制作详细信息?

类似的东西。我不知道您的确切架构:

select 
   p.id, 
   if(IS NULL d2.description, d1.description, d2.description ) `description`
 from product p
 join productDetails d1
   on product.id = productDetails.product_id
      and
   productDetails.language = 'default_lang'
 left join productDetails d2
   on product.id = productDetails.product_id 
      and
   productDetails.language = 'en'