MySQL使用Where选择,如果不存在where条件则默认 [英] Mysql select with Where and default if where condition not present
问题描述
我有表格Product和productDetails.在productDetails中,我有用多种语言描述产品的行(由lang列区分).并非每种产品都有每种语言的描述.如何进行选择,以选择指定语言的描述(通过Where productDescription.lang = 'en'
),如果不存在指定语言,则选择默认语言的描述(始终使用默认语言的描述).
I have tables Product and productDetails. In productDetails I have rows with description of product in many languages (discriminated by lang column). Not every product will have description in every language. How to make a selection that will select the description in the specified language (by Where productDescription.lang = 'en'
) and that will select a description in a default language if the specified language is not present (descriptions with default language are always present).
我做到了:
select *
from product
left join productDetails on product.id = productDetails.product_id
where productDetails.language = 'en';
然后我用英语获得了所有详细信息.如果不存在en,如何修改此项以使用默认语言显示详细信息?
and I get all details in en language. How to modify this to make details in default language selected if en is not present?
推荐答案
类似的东西.我不知道您的确切架构:
Something like that. I don't know your exact schema:
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'
这篇关于MySQL使用Where选择,如果不存在where条件则默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!