我有2个表,由查询说明:
CT_Validation
select ValidationID, Message from CT_Validation
ValidationID Message
======================================================
IP_Validator Not a valid IP address
NumbersOnly Invalid number.
SSN Not a valid social security number.
10Digits A 10-digit number is required.
CT_Validation_Lang
select ValidationID, LangID, Message from CT_Validation_Lang
ValidationID LangID Message
======================================================
SSN es Peligro es mi segundo nombre!
如何建立联接,以便LangID = 'es'
我会回来:
ValidationID Message
======================================================
IP_Validator Not a valid IP address
NumbersOnly Invalid number.
SSN Peligro es mi segundo nombre!
10Digits A 10-digit number is required.
...但如果LangID
为空,空或除“es”以外的任何内容,则结果将恢复为所有英语:
ValidationID Message
======================================================
IP_Validator Not a valid IP address
NumbersOnly Invalid number.
SSN Not a valid social security number.
10Digits A 10-digit number is required.
重要提示:关键字段为ValidationID
,此示例中的重要键值为SSN
,因为它存在于两个表中。
提前感谢任何建议。
答案 0 :(得分:2)
我认为你想要left join
和coalesce
select cv.ValidationID, coalesce(cvl.Message, cv.Message)
from CT_Validation cv
left join CT_Validation_Lang cvl on cv.ValidationID = cvl.ValidationID
and cvl.LangID = 'es'
如果Message
CT_Validation_Lang
存在LangID
,则会从es
中选择Message
。否则它将从CT_Validation
const identityCheck = async (slug) => {
let res;
try {
res = await Bundle.sdk.find(slug);
} catch (err) {
console.log('Fragment didn\'t work ========', slug, err);
try {
res = await Bundle.sdk.find(`package/${slug}`);
} catch (e) {
console.log('Fragment didn\'t work package ========', e);
try {
res = await Bundle.sdk.find(`${slug}-list`);
} catch (error) {
console.log('None of the fragments worked================.', error);
}
}
}
return logResponse(res);
};
identityCheck('fashion');
答案 1 :(得分:1)
使用CASE
SELECT
ctv.ValidationID,
CASE WHEN ctvl.LangID = 'es' THEN ctvl.message ELSE ctv.message END AS Message
FROM CT_Validation ctv
LEFT JOIN CT_Validation_Lang ctvl ON ctv.ValidationId = ctvl.ValidationId
AND ctvl.LangID = 'es'
答案 2 :(得分:0)
你可以这样做:
select v.ValidationID, coalesce(vl.message, v.message) as message
from CT_Validation v left join
CT_Validation_Lang vl
ON vl.ValidationID = v.ValidationID and vl.LangID = 'es';
当西班牙语翻译可用时,这将从CT_Validation_Lang
中选择消息。否则,它会选择原始消息。
答案 3 :(得分:0)
类似的东西: -
select
ctv.ValidationID,
coalesce(
(select
ctvl.Message
from
CT_Validation_Lang ctvl
where
ctvl.ValidationID = ctv.ValidationID and
LangID = 'es'
),
ctv.Message, ' ') as Message
from
CT_Validation ctv;
答案 4 :(得分:0)
将ISNULL()
功能与left join
一起使用。
select a.ValidationID, ISNULL(b.message, a.message)
from CT_Validation a
left join CT_Validation_Lang b on a.ValidationID = b.ValidationID and b.LangID = 'es'