希望在替代条件下进行表连接(此处,替换最初在特定列中找到的数据的子字符串)。所以我基本上想要加入特定列中找到的两种字符串类型之一。我的第一个查询工作得很好。我在下面运行的第二个查询运行,但只运行并运行并永远运行。
首先查询:
select *
from (select count(hls_case.case_id), count(build_opinion.source_id) as build_case_id
from hls_case left join
build_opinion
on replace(substring(build_opinion.source_id, 69, 37), 'redacted_CASEMETS_', '') = hls_case.case_id and
build_opinion.build_name = 'romeo3'
);
第二次查询:
select *
from (select count(hls_case.case_id), count(build_opinion.source_id) as build_case_id
from hls_case left join
build_opinion
on (replace(substring(build_opinion.source_id, 69, 37), 'redacted_CASEMETS_', '') = hls_case.case_id or
replace(substring(build_opinion.source_id, 61, 29), 'redacted_CASEMETS_', '') = hls_case.case_id
) and
build_opinion.build_name = 'romeo3'
);
答案 0 :(得分:0)
我在加入条件中使用UNION
而不是OR
。
select count(hls_case.case_id), count(build_opinion.source_id)
from (
select hls_case.case_id, build_opinion.source_id
from hls_case
left join build_opinion on replace(substring(build_opinion.source_id, 69, 37), 'redacted_CASEMETS_', '') = hls_case.case_id
and build_opinion.build_name = 'romeo3'
union
select hls_case.case_id, build_opinion.source_id
from hls_case
left join build_opinion on replace(substring(build_opinion.source_id, 61, 29), 'redacted_CASEMETS_', '') = hls_case.case_id
and build_opinion.build_name = 'romeo3'
)
答案 1 :(得分:0)
是的,你不想要你的加入OR,除非它是一个非常原始的条件。同样,我避免在两个表之间加入大于/小于联接。
我不会打赌我的生活会产生相同的结果,但它并不会显示两种情况之间可能存在任何重叠,所以我认为你可以做两个左连接,每个条件一个。
这最多只能对每个表进行一次扫描。
select
count(hls_case.case_id),
count(coalesce (bo1.source_id, bo2.source_id)) as build_case_id
from
hls_case
left join build_opinion bo1 on
replace(substring(bo1.source_id, 69, 37), 'redacted_CASEMETS_', '') =
hls_case.case_id and
bo1.build_name = 'romeo3'
left join build_opinion bo2 on
replace(substring(bo2.source_id, 61, 29), 'redacted_CASEMETS_', '') =
hls_case.case_id and
bo2.build_name = 'romeo3'
我对你的数据做了一些合理的假设,但是有可能有重叠不会跳出来,这会导致每个左连接上的1:很多会破坏此