我需要根据inner join
字段值,根据不同条件设置doc_type
。
这是架构
Person (
...
doc_type VARCHAR2(40),
doc_series VARCHAR2(4),
doc_number VARCHAR2(6),
doc_date DATE
)
Document (
id INTEGER,
type VARCHAR2(40),
series VARCHAR2(4),
number VARCHAR2(6),
date DATE
)
例如,如果doc_type
值为01
,我需要按doc_type
和doc_series
列进行加入,如果doc_type
值为02
然后加入应该由doc_type
,doc_series
,doc_number
和doc_date
完成。
像这样的东西
SELECT d.* FROM document d
INNER JOIN person p ON
(CASE when d.doc_type = '01' then
p.doc_type = d.type AND
p.doc_series = d.series
when d.doc_type = '02' then
p.doc_type = d.type AND
p.doc_series = d.series AND
p.doc_number = d.doc_number AND
p.doc_date = d.doc_date
);
如何在Oracle DB中执行此操作?
答案 0 :(得分:1)
我认为你需要这个
SELECT d.*
FROM document d
INNER JOIN person p ON p.doc_series = d.series AND
( d.doc_type = '01' AND p.doc_type = d.type) OR
( d.doc_type = '02' AND p.doc_type = d.type AND p.doc_number = d.doc_number AND
p.doc_date = d.doc_date)
答案 1 :(得分:1)
SELECT d.* FROM document d
INNER JOIN person p ON (
(d.doc_type = '01' AND p.doc_type = d.type AND p.doc_series = d.series)
OR
( d.doc_type = '02' AND p.doc_type = d.type AND p.doc_series = d.series AND
p.doc_number = d.doc_number AND
p.doc_date = d.doc_date)
);
答案 2 :(得分:1)
我宁愿使用union来连接符合条件的表:
SELECT d.* FROM document d
INNER JOIN person p ON
p.doc_type = d.type AND
p.doc_series = d.series
where d.doc_typ = '01'
UNION ALL
SELECT d.*
FROM document d
INNER JOIN person p ON
p.doc_type = d.type AND
p.doc_series = d.series AND
p.doc_number = d.doc_number AND
p.doc_date = d.doc_date
where d.doc_typ = '02'
答案 3 :(得分:1)
如果你真的在寻找CASE,试试这个:
SELECT d.*
FROM document d
INNER JOIN person p ON
(CASE when (p.doc_type = d.type AND
p.doc_series = d.series) then
'01'
when ( p.doc_type = d.type AND
p.doc_series = d.series AND
p.doc_number = d.doc_number AND
p.doc_date = d.doc_date) then
'02'
end ) = p.doc_type;
由于您没有正确提及表中的列名,我使用了您提供的查询。请检查列并运行它。希望它有所帮助。