Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql选择Where,如果Where条件不存在,则选择默认值_Mysql_Selection_Where - Fatal编程技术网

Mysql选择Where,如果Where条件不存在,则选择默认值

Mysql选择Where,如果Where条件不存在,则选择默认值,mysql,selection,where,Mysql,Selection,Where,我有表产品和产品详细信息。在productDetails中,我有许多语言描述产品的行(由lang列区分)。并不是每种产品都有每种语言的描述。如何进行选择,以选择指定语言的描述(byWhere productDescription.lang='en'),并在指定语言不存在时选择默认语言的描述(使用默认语言的描述始终存在) 我做了这个: select * from product left join productDetails on product.id = productDetails.

我有表产品和产品详细信息。在productDetails中,我有许多语言描述产品的行(由lang列区分)。并不是每种产品都有每种语言的描述。如何进行选择,以选择指定语言的描述(by
Where productDescription.lang='en'
),并在指定语言不存在时选择默认语言的描述(使用默认语言的描述始终存在)

我做了这个:

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

我用英语了解所有细节。如果不存在en,如何修改此选项以选择默认语言的详细信息?

类似的内容。我不知道你的确切模式:

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'

欢迎来到SO。请阅读并了解如何创建默认语言代码?在这种情况下,它可能不是
'en'
。我选择的某些语言是网页上的默认语言。如果某些数据没有翻译,则应以默认语言显示。表中没有空值。可能有带有“pl”、“en”、“it”等描述的行,但并非每种语言都是强制性的。我想从指定语言的联接表中选择行,但如果没有此语言的描述,我想选择默认语言(始终存在)的行。当您运行
左联接到描述表时,您将在d2.description列中看到null。然后,您可以通过
选择函数默认值或转换值。
选择p.id,coalesce(d2.description,d1.description)作为描述,从p.id=d1.product_id上的productDetails d1和d1.language='default'左连接p.id=d2上的productDetails d2.product_id和d2.language='en'
工作正常。谢谢