SQL 加入以覆盖左侧的特定列 table
SQL join to override particular column of left table
我有两个 table,即 Table1 和 Table2
表 1
custId--custName--custAge
c1--c1name--32
c2--c2name--41
c3--c3name--41
表2
custId--verified--custName
c1--Y--c1FullName
c2--N--c2FullName
我需要加入 Table1 和 Table2,这样如果在 table 2 中验证的列是 Y,我需要来自 Table2 而不是 Table1 的 custName。
因此,所需的输出是:(如果该 custId 的已验证列为 Y,则覆盖 Table2 中的 custName 列)
custId--custName--custAge
c1--c1FullName--32
c2--c2name--41
c3--c3name--41
我写了以下查询,但没有给出正确的结果。请帮忙。
select T1.custId, NVL(T2.custName, T1.custName),T1.custAge
from Table1 T1
left join Table2 T2 on T1.custId=T2.custId and T2.verified='Y'
您可以使用 CASE
语句来实现:
SELECT tab1.custId,
CASE
WHEN (tab2.verified = 'Y')
THEN tab2.custName
ELSE tab1.custName
END AS CustName,
tab1.custAge
FROM Table1 tab1
LEFT JOIN Table2 tab2 ON tab1.custId = tab2.custId
在此处查看 -> http://rextester.com/EVOMK25746(此 fiddle 构建于 SQL Server
,但是,查询也应该在 Oracle
数据库上工作)
希望对您有所帮助!!!
尝试这个查询一次..
select * into #tab1 from
(
select 'c1' custId,'c1name' custName,31 custAge
union all
select 'c2' ,'c2name' ,41
union all
select 'c3' ,'c3name' ,41
) as a
select * into #tab2 from
(
select 'c1' custId,'Y'verified,'c1FullName' custName
UNION ALL
SELECT 'c2','N','c2FullName '
) as a
SELECT T1.custId,CASE WHEN T2.verified='Y' THEN T2.custName ELSE T1.custName END AS CUSTNAME,T1.custAge FROM #tab1 T1
LEFT JOIN #tab2 T2 ON T1.custId=T2.custId
select custId, custName, custAge
from Table1
natural join
( select custId
from Table2
where verified='N' ) t2
union
select custId, custName, custAge
from ( select custId, custAge from Table1 ) t1
natural join
( select custId, custName
from Table2
where verified='Y' ) t2;
我有两个 table,即 Table1 和 Table2
表 1
custId--custName--custAge
c1--c1name--32
c2--c2name--41
c3--c3name--41
表2
custId--verified--custName
c1--Y--c1FullName
c2--N--c2FullName
我需要加入 Table1 和 Table2,这样如果在 table 2 中验证的列是 Y,我需要来自 Table2 而不是 Table1 的 custName。
因此,所需的输出是:(如果该 custId 的已验证列为 Y,则覆盖 Table2 中的 custName 列)
custId--custName--custAge
c1--c1FullName--32
c2--c2name--41
c3--c3name--41
我写了以下查询,但没有给出正确的结果。请帮忙。
select T1.custId, NVL(T2.custName, T1.custName),T1.custAge
from Table1 T1
left join Table2 T2 on T1.custId=T2.custId and T2.verified='Y'
您可以使用 CASE
语句来实现:
SELECT tab1.custId,
CASE
WHEN (tab2.verified = 'Y')
THEN tab2.custName
ELSE tab1.custName
END AS CustName,
tab1.custAge
FROM Table1 tab1
LEFT JOIN Table2 tab2 ON tab1.custId = tab2.custId
在此处查看 -> http://rextester.com/EVOMK25746(此 fiddle 构建于 SQL Server
,但是,查询也应该在 Oracle
数据库上工作)
希望对您有所帮助!!!
尝试这个查询一次..
select * into #tab1 from
(
select 'c1' custId,'c1name' custName,31 custAge
union all
select 'c2' ,'c2name' ,41
union all
select 'c3' ,'c3name' ,41
) as a
select * into #tab2 from
(
select 'c1' custId,'Y'verified,'c1FullName' custName
UNION ALL
SELECT 'c2','N','c2FullName '
) as a
SELECT T1.custId,CASE WHEN T2.verified='Y' THEN T2.custName ELSE T1.custName END AS CUSTNAME,T1.custAge FROM #tab1 T1
LEFT JOIN #tab2 T2 ON T1.custId=T2.custId
select custId, custName, custAge
from Table1
natural join
( select custId
from Table2
where verified='N' ) t2
union
select custId, custName, custAge
from ( select custId, custAge from Table1 ) t1
natural join
( select custId, custName
from Table2
where verified='Y' ) t2;